1
0
Fork 0

First attempt

This commit is contained in:
Wouter van Wageningen 2023-04-19 06:08:26 +00:00
parent ccf2ff3931
commit 5dcaa15f32
10 changed files with 117 additions and 16 deletions

View file

@ -0,0 +1,94 @@
//////////////////////////////////////////////
// CONSTRUCTOR //
//////////////////////////////////////////////
/**
* Constructor for Defs
*
* @constructor
* @return {Defs} this - The Defs instance
*/
export function Defs() {
this.list = {}
return this
}
//////////////////////////////////////////////
// PUBLIC METHODS //
//////////////////////////////////////////////
/**
* Return a deep copy of this
*
* @return {object} this - The Defs instance
*/
Defs.prototype.clone = function () {
let clone = new Defs()
clone.list = JSON.parse(JSON.stringify(this.list))
return clone
}
/**
* Retrieve a def
*
* @param {string} name - Name of the def to get
* @return value - The value under name
*/
Defs.prototype.get = function (name) {
if (typeof this.list[name] === 'undefined') return false
else return this.list[name]
}
/**
* Remove a def
*
* @param {string} name - Name of the def to set
* @return {object} this - The Defs instance
*/
Defs.prototype.remove = function (name) {
delete this.list[name]
return this
}
/**
* Return SVG code for Defs
*
* @return {string} svg - The SVG code
*/
Defs.prototype.render = function () {
let svg = ''
for (let key in this.list) {
svg += ` ${key}="${this.list[key].join('')}"`
}
return svg
}
/**
* Set a def, overwriting existing value
*
* @param {string} name - Name of the defs to set
* @param {string} value - Value of the defs to set
* @return {Defs} this - The Defs instance
*/
Defs.prototype.set = function (name, value) {
this.list[name] = [value]
return this
}
/**
* Sets a def, but only if it's not currently set
*
* @param {string} name - Name of the def to set
* @param {string} value - Value of the def to set
* @return {Defs} this - The Defs instance
*/
Defs.prototype.setIfUnset = function (name, value) {
if (typeof this.list[name] === 'undefined') this.list[name] = [value]
return this
}

View file

@ -1,4 +1,5 @@
import { Attributes } from './attributes.mjs'
import { Defs } from './defs.mjs'
import { __addNonEnumProp, round } from './utils.mjs'
import { version } from '../data.mjs'
@ -31,7 +32,7 @@ export function Svg(pattern) {
this.layout = {}
this.body = ''
this.style = ''
this.defs = ''
this.defs = new Defs()
}
//////////////////////////////////////////////
@ -215,7 +216,7 @@ Svg.prototype.__renderCircle = function (point) {
Svg.prototype.__renderDefs = function () {
let svg = '<defs>'
this.__indent()
svg += this.__nl() + this.defs
svg += this.__nl() + this.defs.render()
this.__outdent()
svg += this.__nl() + '</defs>' + this.__nl()

View file

@ -1,6 +1,7 @@
const defs = [
// button
`
{
name: 'button',
def: `
<g id="button">
<circle
cx="0" cy="0" r="3.4"
@ -11,8 +12,10 @@ const defs = [
<circle cx="1" cy="1" r="0.5" class="no-stroke fill-mark" />
<circle cx="-1" cy="1" r="0.5" class="no-stroke fill-mark" />
</g>`,
// buttonhole
`
},
{
name: 'buttonhole',
def: `
<g id="buttonhole">
<path
class="mark"
@ -31,8 +34,10 @@ const defs = [
d="M -1,0 L 1,0 L 1,10 L -1,10 z"
/>
</g>`,
// snaps
`
},
{
name: 'snaps',
def: `
<radialGradient id="snap-stud-grad" cx="50%" cy="50%" r="50%" fx="50%" fy="50%">
<stop offset="30%" style="stop-color:rgb(235,235,235); stop-opacity:1"/>
<stop offset="80%" style="stop-color:rgb(100,100,100);stop-opacity:1" />
@ -61,6 +66,7 @@ const defs = [
d="M -1.7,-1 L -1.7,1 M 1.7,-1 L 1.7,1" id="snap-socket-lines"
/>
</g>`,
},
]
// Export hooks
@ -68,7 +74,7 @@ export const buttonsHooks = {
preRender: [
function (svg) {
for (const def of defs) {
if (svg.defs.indexOf(def) === -1) svg.defs += def
svg.defs.setIfUnset(def.name, def.def)
}
},
],

View file

@ -11,7 +11,7 @@ const markers = `
export const cutonfoldHooks = {
preRender: [
function (svg) {
if (svg.defs.indexOf(markers) === -1) svg.defs += markers
svg.defs.setIfUnset('cutonfold', markers)
},
],
}

View file

@ -74,7 +74,7 @@ function lleader(so, type, props, id) {
export const dimensionsHooks = {
preRender: [
function (svg) {
if (svg.defs.indexOf(markers) === -1) svg.defs += markers
svg.defs.setIfUnset('dimensions', markers)
},
],
}

View file

@ -12,7 +12,7 @@ const dflts = { text: 'grainline' }
export const grainlineHooks = {
preRender: [
function (svg) {
if (svg.defs.indexOf(markers) === -1) svg.defs += markers
svg.defs.setIfUnset('grainline', markers)
},
],
}

View file

@ -5,7 +5,7 @@ const logo = (scale) =>
export const logoHooks = {
preRender: [
function (svg) {
if (svg.defs.indexOf('id="logo"') === -1) svg.defs += logo(svg.pattern.settings[0].scale)
svg.defs.setIfUnset('logo', logo(svg.pattern.settings[0].scale))
},
],
}

View file

@ -12,7 +12,7 @@ const markers = `
export const notchesHooks = {
preRender: [
function (svg) {
if (svg.defs.indexOf(`id="notch"`) === -1) svg.defs += markers
svg.defs.setIfUnset('notch', markers)
},
],
}

View file

@ -8,7 +8,7 @@ const markers = `
export const pleatHooks = {
preRender: [
function (svg) {
if (svg.defs.indexOf(markers) === -1) svg.defs += markers
svg.defs.setIfUnset('pleat', markers)
},
],
}

View file

@ -14,7 +14,7 @@ const markers = `
export const sewtogetherHooks = {
preRender: [
function (svg) {
if (svg.defs.indexOf(markers) === -1) svg.defs += markers
svg.defs.setIfUnset('sewTogether', markers)
},
],
}