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 561 of file psd_layer_section.cpp.

562{
563 const KisLayer *layer = qobject_cast<KisLayer *>(node.data());
564 KisPSDLayerStyleSP layerStyle = layer->layerStyle();
565
566 if (!layerStyle)
567 return QDomDocument();
568
570 serializer.setStyles(QVector<KisPSDLayerStyleSP>() << layerStyle);
571 return serializer.formPsdXmlDocument();
572}
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 574 of file psd_layer_section.cpp.

575{
576 return KisDomUtils::findElementByAttribute(parent, "node", "key", key);
577}
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 545 of file psd_layer_section.cpp.

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

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;
511 nodes << item;
512 }
513 } else if (shapeLayer) {
514 if (shapeLayer->shapes().size() > 1) {
515 {
516 FlattenedNode item;
517 item.node = child;
519 nodes << item;
520 }
521 flattenShapes(shapeLayer, shapeLayer->shapes(), nodes);
522 {
523 FlattenedNode item;
524 item.node = child;
526 nodes << item;
527 }
528 } else {
529 FlattenedNode item;
530 item.node = child;
532 nodes << item;
533 }
534 } else if (isLayer) {
535 FlattenedNode item;
536 item.node = child;
538 nodes << item;
539 }
540
541 child = child->nextSibling();
542 }
543}
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 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:722
QTransform absoluteTransformation() const
Definition KoShape.cpp:382
void setTransformation(const QTransform &matrix)
Definition KoShape.cpp:417
virtual KoShape * cloneShape() const
creates a deep copy of the shape or shape's subtree
Definition KoShape.cpp:200
void setVisible(bool on)
Definition KoShape.cpp:972
QString name() const
Definition KoShape.cpp:1150
bool isVisible(bool recursive=true) const
Definition KoShape.cpp:979
qreal transparency(bool recursive=false) const
Definition KoShape.cpp:730
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 579 of file psd_layer_section.cpp.

580{
581 QDomNode srcPatternsNode = findNodeByKey(ResourceType::Patterns, src.documentElement());
582 QDomNode dstPatternsNode = findNodeByKey(ResourceType::Patterns, dst.documentElement());
583
584 if (srcPatternsNode.isNull())
585 return;
586 if (dstPatternsNode.isNull()) {
587 dst = src;
588 return;
589 }
590
591 KIS_ASSERT_RECOVER_RETURN(!srcPatternsNode.isNull());
592 KIS_ASSERT_RECOVER_RETURN(!dstPatternsNode.isNull());
593
594 QDomNode node = srcPatternsNode.firstChild();
595 while (!node.isNull()) {
596 QDomNode importedNode = dst.importNode(node, true);
597 KIS_ASSERT_RECOVER_RETURN(!importedNode.isNull());
598
599 dstPatternsNode.appendChild(importedNode);
600 node = node.nextSibling();
601 }
602}
#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.