Skip to content

Commit

Permalink
Merge pull request #390 from dfpc-coe/icons
Browse files Browse the repository at this point in the history
Alt Icons
  • Loading branch information
ingalls authored Oct 17, 2024
2 parents 36fa91f + 07bb1e4 commit 5f8815b
Show file tree
Hide file tree
Showing 14 changed files with 2,522 additions and 437 deletions.
2 changes: 2 additions & 0 deletions api/lib/models/Icon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export type Icon = {
iconset: string;
type2525b: string;
data: string;
data_alt: string;
path: string;
username: string;
}
Expand Down Expand Up @@ -40,6 +41,7 @@ export default class IconModel extends Modeler<typeof pgschema.Icon> {
iconset: pgschema.Icon.iconset,
type2525b: pgschema.Icon.type2525b,
data: pgschema.Icon.data,
data_alt: pgschema.Icon.data_alt,
path: pgschema.Icon.path,
username: pgschema.Iconset.username
}).from(this.generic)
Expand Down
465 changes: 233 additions & 232 deletions api/lib/schema.ts

Large diffs are not rendered by default.

32 changes: 20 additions & 12 deletions api/lib/sprites.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
import spritesmith from 'spritesmith';
import Vinyl from 'vinyl';
import { promisify } from 'node:util'
import type { Icon } from './schema.js';
import { type InferSelectModel } from 'drizzle-orm';
import { Static } from '@sinclair/typebox';
import { IconResponse } from './types.js';
import Sharp from 'sharp';

const SpriteSmith = promisify(spritesmith.run);

type SpriteConfig = {
name?: string;
useDataAlt?: boolean;
};

export default async function(icons: Array<InferSelectModel<typeof Icon>>, config: SpriteConfig = {}) {
const doc = await SpriteSmith({
src: icons.map((icon) => {
return new Vinyl({
// @ts-expect-error Deal with indexing issue on icon
path: config.name ? icon[config.name] + '.png' : icon.path.replace(/.*?\//, ''),
contents: Buffer.from(icon.data, 'base64'),
})
})
});
export default async function(icons: Array<Static<typeof IconResponse>>, config: SpriteConfig = {}) {
const src = [];
for (const icon of icons) {
const contents = await Sharp(Buffer.from(config.useDataAlt && icon.data_alt ? icon.data_alt : icon.data, 'base64'))
.resize(32)
.png()
.toBuffer();

src.push(new Vinyl({
// @ts-expect-error Deal with indexing issue on icon
path: config.name ? icon[config.name] + '.png' : icon.path.replace(/.*?\//, ''),
contents
}))
}

const doc = await SpriteSmith({ src });

const coords: Record<string, any> = {};
for (const key in doc.coordinates) {
Expand Down
6 changes: 6 additions & 0 deletions api/migrations/0067_strange_lifeguard.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
UPDATE settings
SET value = Replace(value, '"', '')
WHERE Starts_With(key, 'group:');

ALTER TABLE icons
ADD COLUMN data_alt TEXT;
Loading

0 comments on commit 5f8815b

Please sign in to comment.