Skip to content

Commit

Permalink
fix sync pictogram folder creation
Browse files Browse the repository at this point in the history
  • Loading branch information
juanda99 committed Dec 1, 2023
1 parent 71eb743 commit 62385d0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 42 deletions.
15 changes: 9 additions & 6 deletions createPNGFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ const {
convertSVG,
modifySVG,
hasHair,
hasSkin
hasSkin,
} = require('./utils/svg')

const SVG_DIR = '/svg'
const IMAGE_DIR = process.env.IMAGE_DIR || '/pictograms'
// env variable come as a string! Be careful!!
const overwrite = process.env.OVERWRITE === '1'

Expand All @@ -24,9 +25,11 @@ const createPNGFiles = async (file, resolution) => {

try {
const svgContent = await fs.readFile(path.resolve(SVG_DIR, file), 'utf-8')
const idFile = path.basename(file, '.svg')
fs.ensureDirSync(path.resolve(IMAGE_DIR, idFile))
const withHair = hasHair(svgContent)
const withSkin = hasSkin(svgContent)
optionsArray.forEach(async options => {
optionsArray.forEach(async (options) => {
if (options.hair && !withHair) return
if (options.skin && !withSkin) return
const fileName = await getPNGFileName(file, options)
Expand All @@ -36,12 +39,12 @@ const createPNGFiles = async (file, resolution) => {
/* if we need to generate with a different hair or skin and code is not inside the svg, we don't use it */
let newSVGContent = modifySVG(svgContent, options)
convertSVG(newSVGContent, options.resolution)
.then(buffer =>
.then((buffer) =>
imagemin.buffer(buffer, {
plugins: [imageminPngquant({ quality: '65-80', speed: 10 })]
plugins: [imageminPngquant({ quality: '65-80', speed: 10 })],
})
)
.then(buffer => {
.then((buffer) => {
fs.open(fileName, 'w', function (err, fd) {
if (err) {
throw new Error(`could not open file: ${err}`)
Expand All @@ -53,7 +56,7 @@ const createPNGFiles = async (file, resolution) => {
})
})
})
.catch(err => {
.catch((err) => {
logger.error(`ERROR GENERATING: ${fileName}: ${err.message}`)
})
}
Expand Down
74 changes: 38 additions & 36 deletions utils/svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const skin = {
black: '#A65C17',
assian: '#F4ECAD',
mulatto: '#E3AB72',
aztec: '#CF9D7C'
aztec: '#CF9D7C',
}

const schematic = '#FEFEFE'
Expand All @@ -23,17 +23,17 @@ const hair = {
black: '#020100',
gray: '#EFEFEF',
darkGray: '#AAABAB',
darkBrown: '#6A2703'
darkBrown: '#6A2703',
}

/* variations we need depending on resolution */
const preCompiledOptions = {
300: { plural: true, action: true },
500: { plural: true, action: true, color: true, peopleAppearance: true },
2500: { color: true, peopleAppearance: true }
2500: { color: true, peopleAppearance: true },
}

const getNextLayer = layer => {
const getNextLayer = (layer) => {
const layers = ['Fondo', 'contorno2', 'relleno', 'contorno']
const layerIndex = layers.indexOf(layer) + 1
if (layerIndex < 4) {
Expand Down Expand Up @@ -68,6 +68,8 @@ const getPNGFileName = async (file, options) => {
if (hair) fileName = `${fileName}_hair-${hair.substr(1, 6)}`
if (skin) fileName = `${fileName}_skin-${skin.substr(1, 6)}`
fileName = `${fileName}_${resolution}`
// added line to createPNGfiles, here not needed now,
// but was not working here???
await fs.ensureDir(path.resolve(IMAGE_DIR, idFile))
return path.resolve(IMAGE_DIR, idFile, `${fileName}.png`)
}
Expand All @@ -76,25 +78,25 @@ const getPeopleAppearanceOptions = (defaultValues, initOptions) => {
const options = []
// add skins:
options.push(
Object.keys(skin).map(skinType => ({
Object.keys(skin).map((skinType) => ({
skin: skin[skinType],
...defaultValues
...defaultValues,
}))
)

options.push(
Object.keys(hair).map(hairType => ({
Object.keys(hair).map((hairType) => ({
hair: hair[hairType],
...defaultValues
...defaultValues,
}))
)
options.push(
flatten(
Object.keys(skin).map(skinType =>
Object.keys(hair).map(hairType => ({
Object.keys(skin).map((skinType) =>
Object.keys(hair).map((hairType) => ({
hair: hair[hairType],
skin: skin[skinType],
...defaultValues
...defaultValues,
}))
)
)
Expand All @@ -103,60 +105,60 @@ const getPeopleAppearanceOptions = (defaultValues, initOptions) => {
if (initOptions.plural) {
// add skins:
options.push(
Object.keys(skin).map(skinType => ({
Object.keys(skin).map((skinType) => ({
skin: skin[skinType],
...defaultValues,
plural: true
plural: true,
}))
)

options.push(
Object.keys(hair).map(hairType => ({
Object.keys(hair).map((hairType) => ({
hair: hair[hairType],
...defaultValues,
plural: true
plural: true,
}))
)
options.push(
flatten(
Object.keys(skin).map(skinType =>
Object.keys(hair).map(hairType => ({
Object.keys(skin).map((skinType) =>
Object.keys(hair).map((hairType) => ({
hair: hair[hairType],
skin: skin[skinType],
...defaultValues,
plural: true
plural: true,
}))
)
)
)
}

if (initOptions.action) {
['past', 'future'].forEach(action => {
;['past', 'future'].forEach((action) => {
// add skins:
options.push(
Object.keys(skin).map(skinType => ({
Object.keys(skin).map((skinType) => ({
skin: skin[skinType],
...defaultValues,
action
action,
}))
)

options.push(
Object.keys(hair).map(hairType => ({
Object.keys(hair).map((hairType) => ({
hair: hair[hairType],
...defaultValues,
action
action,
}))
)
options.push(
flatten(
Object.keys(skin).map(skinType =>
Object.keys(hair).map(hairType => ({
Object.keys(skin).map((skinType) =>
Object.keys(hair).map((hairType) => ({
hair: hair[hairType],
skin: skin[skinType],
...defaultValues,
action
action,
}))
)
)
Expand All @@ -166,17 +168,17 @@ const getPeopleAppearanceOptions = (defaultValues, initOptions) => {
return flatten(options)
}

const getOptions = resolution => {
const getOptions = (resolution) => {
const initOptions = preCompiledOptions[resolution]
const colors = [true]
if (initOptions.color) colors.push(false)
const options = []
const defaultValues = {
resolution,
color: true,
action: 'present'
action: 'present',
}
colors.forEach(color => {
colors.forEach((color) => {
options.push({ ...defaultValues, color })
if (initOptions.plural) {
options.push({ ...defaultValues, plural: true, color })
Expand Down Expand Up @@ -204,7 +206,7 @@ const modifySkin = (fileContent, key) => {
return fileContent.replace(reSkin, skin[key] || key)
}

const hasSkin = fileContent => {
const hasSkin = (fileContent) => {
const reSkin = new RegExp(skinsToRemove, 'gim')
return reSkin.test(fileContent)
}
Expand All @@ -224,7 +226,7 @@ const modifyHair = (fileContent, key) => {
const reHair = new RegExp(hairToRemove(), 'gim')
return fileContent.replace(reHair, hair[key] || key)
}
const hasHair = fileContent => {
const hasHair = (fileContent) => {
const reHair = new RegExp(hairToRemove(), 'gim')
return reHair.test(fileContent)
}
Expand All @@ -241,7 +243,9 @@ const modifySVG = (fileContent, options) => {
}
if (!color) content = modifyLayer(content, 'relleno', '')
if (action === 'future') content = addLayer(content, 'action', futureSVGCode)
else if (action === 'past') { content = addLayer(content, 'action', pastSVGCode) }
else if (action === 'past') {
content = addLayer(content, 'action', pastSVGCode)
}
if (hair) content = modifyHair(content, hair)
if (skin) content = modifySkin(content, skin)

Expand All @@ -253,9 +257,7 @@ const convertSVG = (fileContent, resolution) => {
// density 450p is for 3125x image
const density = parseInt(0.144 * resolution, 10)
const fileBuffer = Buffer.from(fileContent)
return sharp(fileBuffer, { density })
.png()
.toBuffer()
return sharp(fileBuffer, { density }).png().toBuffer()
}

module.exports = {
Expand All @@ -265,5 +267,5 @@ module.exports = {
modifySVG,
hasSkin,
hasHair,
preCompiledOptions
preCompiledOptions,
}

0 comments on commit 62385d0

Please sign in to comment.