426 def handleShapeDescription(self, shape, list, textOnly=False):
427 if (shape.type() != "KoSvgTextShapeID" and textOnly is True):
428 return
429 shapeDesc = {}
430 shapeDesc["name"] = shape.name()
431 rect = shape.boundingBox()
432 listOfPoints = [rect.topLeft(), rect.topRight(), rect.bottomRight(), rect.bottomLeft()]
433 shapeDesc["boundingBox"] = listOfPoints
434 if (shape.type() == "KoSvgTextShapeID" and textOnly is True):
435 shapeDesc["text"] = shape.toSvg()
436 list.append(shapeDesc)
437 return
438
439 r"""
440 shapeDesc = {}
441 shapeDesc["name"] = shape.name()
442 rect = shape.boundingBox()
443 listOfPoints = [rect.topLeft(), rect.topRight(), rect.bottomRight(), rect.bottomLeft()]
444 shapeDoc = minidom.parseString(shape.toSvg())
445 docElem = shapeDoc.documentElement
446 svgRegExp = re.compile(r'[MLCSQHVATmlzcqshva]\d+\.?\d* \d+\.?\d*')
447 transform = docElem.getAttribute("transform")
448 coord = []
449 adjust = QTransform()
450 # TODO: If we get global transform api, use that instead of parsing manually.
451 if "translate" in transform:
452 transform = transform.replace('translate(', '')
453 for c in transform[:-1].split(" "):
454 if "," in c:
455 c = c.replace(",", "")
456 coord.append(float(c))
457 if len(coord) < 2:
458 coord.append(coord[0])
459 adjust = QTransform(1, 0, 0, 1, coord[0], coord[1])
460 if "matrix" in transform:
461 transform = transform.replace('matrix(', '')
462 for c in transform[:-1].split(" "):
463 if "," in c:
464 c = c.replace(",", "")
465 coord.append(float(c))
466 adjust = QTransform(coord[0], coord[1], coord[2], coord[3], coord[4], coord[5])
467 path = QPainterPath()
468 if docElem.localName == "path":
469 dVal = docElem.getAttribute("d")
470 listOfSvgStrings = [" "]
471 listOfSvgStrings = svgRegExp.findall(dVal)
472 if listOfSvgStrings:
473 listOfPoints = []
474 for l in listOfSvgStrings:
475 line = l[1:]
476 coordinates = line.split(" ")
477 if len(coordinates) < 2:
478 coordinates.append(coordinates[0])
479 x = float(coordinates[-2])
480 y = float(coordinates[-1])
481 offset = QPointF()
482 if l.islower():
483 offset = listOfPoints[0]
484 if l.lower().startswith("m"):
485 path.moveTo(QPointF(x, y) + offset)
486 elif l.lower().startswith("h"):
487 y = listOfPoints[-1].y()
488 path.lineTo(QPointF(x, y) + offset)
489 elif l.lower().startswith("v"):
490 x = listOfPoints[-1].x()
491 path.lineTo(QPointF(x, y) + offset)
492 elif l.lower().startswith("c"):
493 path.cubicTo(coordinates[0], coordinates[1], coordinates[2], coordinates[3], x, y)
494 else:
495 path.lineTo(QPointF(x, y) + offset)
496 path.setFillRule(Qt.FillRule.WindingFill)
497 for polygon in path.simplified().toSubpathPolygons(adjust):
498 for point in polygon:
499 listOfPoints.append(point)
500 elif docElem.localName == "rect":
501 listOfPoints = []
502 if (docElem.hasAttribute("x")):
503 x = float(docElem.getAttribute("x"))
504 else:
505 x = 0
506 if (docElem.hasAttribute("y")):
507 y = float(docElem.getAttribute("y"))
508 else:
509 y = 0
510 w = float(docElem.getAttribute("width"))
511 h = float(docElem.getAttribute("height"))
512 path.addRect(QRectF(x, y, w, h))
513 for point in path.toFillPolygon(adjust):
514 listOfPoints.append(point)
515 elif docElem.localName == "ellipse":
516 listOfPoints = []
517 if (docElem.hasAttribute("cx")):
518 x = float(docElem.getAttribute("cx"))
519 else:
520 x = 0
521 if (docElem.hasAttribute("cy")):
522 y = float(docElem.getAttribute("cy"))
523 else:
524 y = 0
525 ry = float(docElem.getAttribute("ry"))
526 rx = float(docElem.getAttribute("rx"))
527 path.addEllipse(QPointF(x, y), rx, ry)
528 for point in path.toFillPolygon(adjust):
529 listOfPoints.append(point)
530 elif docElem.localName == "text":
531 # NOTE: This only works for horizontal preformated text. Vertical text needs a different
532 # ordering of the rects, and wraparound should try to take the shape it is wrapped in.
533 family = "sans-serif"
534 if docElem.hasAttribute("font-family"):
535 family = docElem.getAttribute("font-family")
536 size = "11"
537 if docElem.hasAttribute("font-size"):
538 size = docElem.getAttribute("font-size")
539 multilineText = True
540 for el in docElem.childNodes:
541 if el.nodeType == minidom.Node.TEXT_NODE:
542 multilineText = False
543 if multilineText:
544 listOfPoints = []
545 listOfRects = []
546
547 # First we collect all the possible line-rects.
548 for el in docElem.childNodes:
549 if docElem.hasAttribute("font-family"):
550 family = docElem.getAttribute("font-family")
551 if docElem.hasAttribute("font-size"):
552 size = docElem.getAttribute("font-size")
553 fontsize = int(size)
554 font = QFont(family, fontsize)
555 string = el.toxml()
556 string = re.sub(r"<.*?>", " ", string)
557 string = string.replace(" ", " ")
558 width = min(QFontMetrics(font).horizontalAdvance(string.strip()), rect.width())
559 height = QFontMetrics(font).height()
560 anchor = "start"
561 if docElem.hasAttribute("text-anchor"):
562 anchor = docElem.getAttribute("text-anchor")
563 top = rect.top()
564 if len(listOfRects)>0:
565 top = listOfRects[-1].bottom()
566 if anchor == "start":
567 spanRect = QRectF(rect.left(), top, width, height)
568 listOfRects.append(spanRect)
569 elif anchor == "end":
570 spanRect = QRectF(rect.right()-width, top, width, height)
571 listOfRects.append(spanRect)
572 else:
573 # Middle
574 spanRect = QRectF(rect.center().x()-(width*0.5), top, width, height)
575 listOfRects.append(spanRect)
576 # Now we have all the rects, we can check each and draw a
577 # polygon around them.
578 heightAdjust = (rect.height()-(listOfRects[-1].bottom()-rect.top()))/len(listOfRects)
579 for i in range(len(listOfRects)):
580 span = listOfRects[i]
581 addtionalHeight = i*heightAdjust
582 if i == 0:
583 listOfPoints.append(span.topLeft())
584 listOfPoints.append(span.topRight())
585 else:
586 if listOfRects[i-1].width()< span.width():
587 listOfPoints.append(QPointF(span.right(), span.top()+addtionalHeight))
588 listOfPoints.insert(0, QPointF(span.left(), span.top()+addtionalHeight))
589 else:
590 bottom = listOfRects[i-1].bottom()+addtionalHeight-heightAdjust
591 listOfPoints.append(QPointF(listOfRects[i-1].right(), bottom))
592 listOfPoints.insert(0, QPointF(listOfRects[i-1].left(), bottom))
593 listOfPoints.append(QPointF(span.right(), rect.bottom()))
594 listOfPoints.insert(0, QPointF(span.left(), rect.bottom()))
595 path = QPainterPath()
596 path.moveTo(listOfPoints[0])
597 for p in range(1, len(listOfPoints)):
598 path.lineTo(listOfPoints[p])
599 path.closeSubpath()
600 listOfPoints = []
601 for point in path.toFillPolygon(adjust):
602 listOfPoints.append(point)
603 shapeDesc["boundingBox"] = listOfPoints
604 if (shape.type() == "KoSvgTextShapeID" and textOnly is True):
605 shapeDesc["text"] = shape.toSvg()
606 list.append(shapeDesc)
607 """
608