AgentSkillsCN

pagx-optimize

通过去除冗余、合并共享绘图资源、提取可复用资源,并提升代码可读性,优化PAGX文件结构。当用户要求优化、简化或清理PAGX文件时,可使用此技能。

SKILL.md
--- frontmatter
name: pagx-optimize
description: Optimize PAGX file structure by removing redundancy, merging shared painters, extracting reusable resources, and improving readability. Use this skill when asked to optimize, simplify, or clean up a PAGX file.
argument-hint: "[file-path]"

PAGX File Structure Optimization

This skill provides a systematic checklist for optimizing PAGX file structure. The goals are to simplify file structure, reduce file size, and improve rendering performance.

Fundamental Constraint

All optimizations must preserve the original design appearance. This is a hard requirement that overrides any individual optimization direction below.

  • Allowed: Structural transformations that produce identical or near-identical rendering (minor pixel-level differences from node reordering or painter merging are acceptable).
  • Allowed: Removing provably invisible content — elements entirely outside the canvas bounds, unused resources, zero-width strokes, fully transparent elements, and other content that contributes nothing to the final rendered image.
  • Forbidden: Modifying any parameter that affects design intent — blur radius, spacing, density, colors, alpha, gradient stops, font sizes, shadow offsets, stroke widths, or any other visual attribute — unless the user explicitly approves the change.
  • Forbidden: Reducing Repeater density (increasing spacing), lowering blur values, changing opacity, or simplifying geometry in ways that alter the visual result, even if the change seems minor. These are design decisions, not optimization decisions.
  • When in doubt: If an optimization might change the rendered appearance, do not apply it. Instead, describe the potential optimization and its visual impact to the user, and ask for explicit approval before proceeding.

Optimization Checklist

Structure Cleanup (Sections 1-7)

#OptimizationWhen to Apply
1Move Resources to End<Resources> appears before layer tree
2Remove Empty ElementsEmpty <Layer/>, empty <Resources>, width="0" strokes
3Omit Default ValuesAttributes explicitly set to spec default
4Simplify TransformsTranslation-only matrix, identity matrix, cascaded translations
5Normalize NumericsScientific notation near zero, trailing decimals, short hex
6Remove Unused ResourcesResource id has no @id reference
7Remove Redundant WrappersGroup/Layer with no attributes wrapping single element

For detailed examples and default value tables, read references/structure-cleanup.md.

Painter Merging (Sections 8-9)

#OptimizationWhen to Apply
8Merge Geometry Sharing Identical PaintersMultiple geometry elements use same Fill/Stroke
9Merge Painters on Identical GeometrySame geometry appears twice with different painters

Critical caveat (Section 8): Different geometry needing different painters must be isolated with Groups. This is the most common source of errors.

For detailed examples and scope isolation patterns, read references/painter-merging.md.

Resource Reuse (Sections 10-14)

#OptimizationWhen to Apply
10Composition Reuse2+ identical layer subtrees differing only in position
11PathData ReuseSame path data string appears 2+ times
12Color Source SharingIdentical gradient definitions inline in multiple places
13Replace Path with PrimitivePath describes a Rectangle or Ellipse
14Remove Full-Canvas ClipsClip mask covers entire canvas

For detailed examples and coordinate conversion formulas, read references/resource-reuse.md.

Performance Optimization (Section 15)

Rendering Cost Model:

  • Repeater: N copies = N× full rendering cost (no GPU instancing)
  • Nested Repeaters: Multiplicative (A×B elements)
  • BlurFilter / DropShadowStyle: Cost proportional to blur radius
  • Dashed Stroke under Repeater: Dash computation per copy
  • Layer vs Group: Layer creates extra surface; Group is lighter

Two categories:

  1. Equivalent (auto-apply): Downgrade Layer to Group, clip Repeater to canvas bounds
  2. Suggestions (never auto-apply): Reduce density, lower blur, simplify geometry

For detailed optimization techniques, read references/performance.md.


Appendix: Core Concepts

Painter Scope

Painters (Fill / Stroke) render all geometry accumulated in the current scope up to that painter's position. Subsequent painters continue to render the same geometry.

Group Scope Isolation

Group creates an isolated scope. Internal geometry accumulates only within the Group, and after the Group ends, its geometry propagates upward to the parent scope.

Layer vs Group

FeatureLayerGroup
Geometry propagationNo (boundary)Yes (to parent)
Styles / Filters / MaskSupportedNot supported
Composition / BlendModeSupportedNot supported
Transformmatrixposition/rotation/scale

Selection rule: Use Layer for styles/filters/mask/composition/blendMode. Otherwise prefer Group.

For DropShadowStyle scope and color source coordinate system, see references/painter-merging.md and references/resource-reuse.md respectively.


PAGX Specification Quick Reference

For complete attribute defaults, required attributes, and enumeration values extracted from the PAGX spec, see references/pagx-quick-reference.md.