Krita Source Code Documentation
Loading...
Searching...
No Matches
psd_layer_section.cpp File Reference
#include "psd_layer_section.h"
#include <QBuffer>
#include <QIODevice>
#include <KoColor.h>
#include <KoColorSpace.h>
#include <kis_debug.h>
#include <kis_effect_mask.h>
#include <kis_group_layer.h>
#include <kis_generator_layer.h>
#include <kis_image.h>
#include <kis_node.h>
#include <kis_paint_layer.h>
#include <kis_painter.h>
#include <kis_selection.h>
#include <kis_shape_selection.h>
#include <kis_transparency_mask.h>
#include <kis_shape_layer.h>
#include <KoSvgTextShape.h>
#include <KoShapeBackground.h>
#include <KoColorBackground.h>
#include <KoPatternBackground.h>
#include <KoGradientBackground.h>
#include <KoShapeStroke.h>
#include <KoPathShape.h>
#include <KoShapeGroup.h>
#include <KoShapeManager.h>
#include <KoSvgTextShapeMarkupConverter.h>
#include "kis_dom_utils.h"
#include "psd.h"
#include "psd_header.h"
#include "psd_utils.h"
#include "compression.h"
#include <asl/kis_asl_reader_utils.h>
#include <asl/kis_asl_writer_utils.h>
#include <asl/kis_offset_on_exit_verifier.h>
#include <kis_asl_layer_style_serializer.h>
#include <cos/kis_txt2_utls.h>
#include <cos/psd_text_data_converter.h>

Go to the source code of this file.

Classes

struct  FlattenedNode
 

Functions

void addBackgroundIfNeeded (KisNodeSP root, QList< FlattenedNode > &nodes)
 
QDomDocument fetchLayerStyleXmlData (KisNodeSP node)
 
QDomNode findNodeByKey (const QString &key, QDomNode parent)
 
KisNodeSP findOnlyTransparencyMask (KisNodeSP node, FlattenedNode::Type type)
 
void flattenNodes (KisNodeSP node, QList< FlattenedNode > &nodes)
 
void flattenShapes (const KisShapeLayer *parentShapeLayer, QList< KoShape * > shapes, QList< FlattenedNode > &nodes)
 
void mergePatternsXMLSection (const QDomDocument &src, QDomDocument &dst)
 

Function Documentation

◆ addBackgroundIfNeeded()

void addBackgroundIfNeeded ( KisNodeSP root,
QList< FlattenedNode > & nodes )

Definition at line 420 of file psd_layer_section.cpp.

421{
422 KisGroupLayer *group = dynamic_cast<KisGroupLayer *>(root.data());
423 if (!group)
424 return;
425
426 KoColor projectionColor = group->defaultProjectionColor();
427 if (projectionColor.opacityU8() == OPACITY_TRANSPARENT_U8)
428 return;
429
430 KisPaintLayerSP layer = new KisPaintLayer(group->image(), i18nc("Automatically created layer name when saving into PSD", "Background"), OPACITY_OPAQUE_U8);
431
432 layer->paintDevice()->setDefaultPixel(projectionColor);
433
434 {
435 FlattenedNode item;
436 item.node = layer;
438 nodes << item;
439 }
440}
const quint8 OPACITY_TRANSPARENT_U8
const quint8 OPACITY_OPAQUE_U8
void setDefaultPixel(const KoColor &defPixel)
quint8 opacityU8() const
Definition KoColor.cpp:341
KisImageWSP image
KoColor defaultProjectionColor() const
KisPaintDeviceSP paintDevice

References KisSharedPtr< T >::data(), KisGroupLayer::defaultProjectionColor(), KisBaseNode::image, FlattenedNode::node, OPACITY_OPAQUE_U8, OPACITY_TRANSPARENT_U8, KoColor::opacityU8(), KisPaintLayer::paintDevice, FlattenedNode::RASTER_LAYER, KisPaintDevice::setDefaultPixel(), and FlattenedNode::type.

◆ fetchLayerStyleXmlData()

QDomDocument fetchLayerStyleXmlData ( KisNodeSP node)

Definition at line 565 of file psd_layer_section.cpp.

566{
567 const KisLayer *layer = qobject_cast<KisLayer *>(node.data());
568 KisPSDLayerStyleSP layerStyle = layer->layerStyle();
569
570 if (!layerStyle)
571 return QDomDocument();
572
574 serializer.setStyles(QVector<KisPSDLayerStyleSP>() << layerStyle);
575 return serializer.formPsdXmlDocument();
576}
void setStyles(const QVector< KisPSDLayerStyleSP > &styles)
KisPSDLayerStyleSP layerStyle
Definition kis_layer.cc:171

References KisSharedPtr< T >::data(), KisAslLayerStyleSerializer::formPsdXmlDocument(), KisLayer::layerStyle, and KisAslLayerStyleSerializer::setStyles().

◆ findNodeByKey()

QDomNode findNodeByKey ( const QString & key,
QDomNode parent )
inline

Definition at line 578 of file psd_layer_section.cpp.

579{
580 return KisDomUtils::findElementByAttribute(parent, "node", "key", key);
581}
QDomElement findElementByAttribute(QDomNode parent, const QString &tag, const QString &attribute, const QString &value)

References KisDomUtils::findElementByAttribute().

◆ findOnlyTransparencyMask()

KisNodeSP findOnlyTransparencyMask ( KisNodeSP node,
FlattenedNode::Type type )

Definition at line 549 of file psd_layer_section.cpp.

550{
552 return 0;
553 }
554
555 KisLayer *layer = qobject_cast<KisLayer *>(node.data());
556 QList<KisEffectMaskSP> masks = layer->effectMasks();
557
558 if (masks.size() != 1)
559 return 0;
560
561 KisEffectMaskSP onlyMask = masks.first();
562 return onlyMask->inherits("KisTransparencyMask") ? onlyMask : 0;
563}
QList< KisEffectMaskSP > effectMasks() const
Definition kis_layer.cc:527

References KisSharedPtr< T >::data(), KisLayer::effectMasks(), FlattenedNode::FOLDER_CLOSED, FlattenedNode::FOLDER_OPEN, and FlattenedNode::RASTER_LAYER.

◆ flattenNodes()

void flattenNodes ( KisNodeSP node,
QList< FlattenedNode > & nodes )

Definition at line 489 of file psd_layer_section.cpp.

490{
491 KisNodeSP child = node->firstChild();
492 while (child) {
493 const bool isLayer = child->inherits("KisLayer");
494 const bool isGroupLayer = child->inherits("KisGroupLayer");
495 const KisShapeLayer *shapeLayer = qobject_cast<KisShapeLayer*>(child.data());
496
497 if (isGroupLayer) {
498 {
499 FlattenedNode item;
500 item.node = child;
502 nodes << item;
503 }
504
505 flattenNodes(child, nodes);
506
507 {
508 FlattenedNode item;
509 item.node = child;
510 if (child->collapsed()) {
512 } else {
514 }
515 nodes << item;
516 }
517 } else if (shapeLayer) {
518 if (shapeLayer->shapes().size() > 1) {
519 {
520 FlattenedNode item;
521 item.node = child;
523 nodes << item;
524 }
525 flattenShapes(shapeLayer, shapeLayer->shapes(), nodes);
526 {
527 FlattenedNode item;
528 item.node = child;
530 nodes << item;
531 }
532 } else {
533 FlattenedNode item;
534 item.node = child;
536 nodes << item;
537 }
538 } else if (isLayer) {
539 FlattenedNode item;
540 item.node = child;
542 nodes << item;
543 }
544
545 child = child->nextSibling();
546 }
547}
QList< KoShape * > shapes() const
static bool isLayer(KisNodeSP node)
void flattenShapes(const KisShapeLayer *parentShapeLayer, QList< KoShape * > shapes, QList< FlattenedNode > &nodes)
void flattenNodes(KisNodeSP node, QList< FlattenedNode > &nodes)
KisNodeSP firstChild() const
Definition kis_node.cpp:361
KisNodeSP nextSibling() const
Definition kis_node.cpp:408

References KisBaseNode::collapsed, KisSharedPtr< T >::data(), KisNode::firstChild(), flattenNodes(), flattenShapes(), FlattenedNode::FOLDER_CLOSED, FlattenedNode::FOLDER_OPEN, isLayer(), KisNode::nextSibling(), FlattenedNode::node, FlattenedNode::RASTER_LAYER, FlattenedNode::SECTION_DIVIDER, KoShapeContainer::shapes(), and FlattenedNode::type.

◆ flattenShapes()

void flattenShapes ( const KisShapeLayer * parentShapeLayer,
QList< KoShape * > shapes,
QList< FlattenedNode > & nodes )

Definition at line 442 of file psd_layer_section.cpp.

442 {
443 Q_FOREACH (KoShape *shape, shapes) {
444 const QString name = shape->name().isEmpty()? "shape "+QString::number(nodes.size()): shape->name();
445 KoShapeGroup *group = dynamic_cast<KoShapeGroup*>(shape);
446 if (group) {
447 KisGroupLayerSP newGroup(new KisGroupLayer(parentShapeLayer->image(),
448 name,
449 (1.0-shape->transparency(false))*255,
450 parentShapeLayer->colorSpace()));
451 newGroup->setVisible(shape->isVisible(false));
452 newGroup->setOpacity(shape->transparency(false)*255);
453 {
454 FlattenedNode item;
455 item.node = newGroup;
457 nodes << item;
458 }
459 flattenShapes(parentShapeLayer, group->shapes(), nodes);
460 {
461 FlattenedNode item;
462 item.node = newGroup;
464 nodes << item;
465 }
466 } else {
467 KisShapeLayerSP newLayer(new KisShapeLayer(nullptr,
468 parentShapeLayer->image(),
469 name,
470 (1.0-shape->transparency(false))*255));
471 KoShape *newShape = shape->cloneShape();
472 newShape->setTransparency(0.0);
473 newShape->setTransformation(shape->absoluteTransformation());
474 newLayer->setVisible(newShape->isVisible(false));
475 newShape->setVisible(true);
476 newLayer->shapeManager()->addShape(newShape, KoShapeManager::AddWithoutRepaint);
477 newLayer->addShape(newShape);
478
479 {
480 FlattenedNode item;
481 item.node = newLayer;
483 nodes << item;
484 }
485 }
486 }
487}
@ AddWithoutRepaint
Avoids each shapes 'update()' to be called for faster addition when its possible.
void setTransparency(qreal transparency)
Definition KoShape.cpp:637
QTransform absoluteTransformation() const
Definition KoShape.cpp:330
void setTransformation(const QTransform &matrix)
Definition KoShape.cpp:369
virtual KoShape * cloneShape() const
creates a deep copy of the shape or shape's subtree
Definition KoShape.cpp:173
void setVisible(bool on)
Definition KoShape.cpp:790
QString name() const
Definition KoShape.cpp:950
bool isVisible(bool recursive=true) const
Definition KoShape.cpp:797
qreal transparency(bool recursive=false) const
Definition KoShape.cpp:645
const char * name(StandardAction id)
const KoColorSpace * colorSpace() const override
returns the image's colorSpace or null, if there is no image
Definition kis_layer.cc:225

References KoShape::absoluteTransformation(), KoShapeContainer::addShape(), KoShapeManager::addShape(), KoShapeManager::AddWithoutRepaint, KoShape::cloneShape(), KisLayer::colorSpace(), flattenShapes(), FlattenedNode::FOLDER_CLOSED, KisBaseNode::image, KoShape::isVisible(), KoShape::name(), FlattenedNode::node, FlattenedNode::RASTER_LAYER, FlattenedNode::SECTION_DIVIDER, KisBaseNode::setOpacity(), KoShape::setTransformation(), KoShape::setTransparency(), KoShape::setVisible(), KisShapeLayer::setVisible(), KisBaseNode::setVisible(), KisShapeLayer::shapeManager(), KoShapeContainer::shapes(), KoShape::transparency(), and FlattenedNode::type.

◆ mergePatternsXMLSection()

void mergePatternsXMLSection ( const QDomDocument & src,
QDomDocument & dst )

Definition at line 583 of file psd_layer_section.cpp.

584{
585 QDomNode srcPatternsNode = findNodeByKey(ResourceType::Patterns, src.documentElement());
586 QDomNode dstPatternsNode = findNodeByKey(ResourceType::Patterns, dst.documentElement());
587
588 if (srcPatternsNode.isNull())
589 return;
590 if (dstPatternsNode.isNull()) {
591 dst = src;
592 return;
593 }
594
595 KIS_ASSERT_RECOVER_RETURN(!srcPatternsNode.isNull());
596 KIS_ASSERT_RECOVER_RETURN(!dstPatternsNode.isNull());
597
598 QDomNode node = srcPatternsNode.firstChild();
599 while (!node.isNull()) {
600 QDomNode importedNode = dst.importNode(node, true);
601 KIS_ASSERT_RECOVER_RETURN(!importedNode.isNull());
602
603 dstPatternsNode.appendChild(importedNode);
604 node = node.nextSibling();
605 }
606}
#define KIS_ASSERT_RECOVER_RETURN(cond)
Definition kis_assert.h:75
const QString Patterns
QDomNode findNodeByKey(const QString &key, QDomNode parent)

References findNodeByKey(), KIS_ASSERT_RECOVER_RETURN, and ResourceType::Patterns.