@@ -6,7 +6,7 @@ export const kGetIconBlob = Symbol('getIcon')
66/**
77 * @typedef {Object } BitmapOpts
88 * @property {Extract<IconVariant['mimeType'], 'image/png'> } mimeType
9- * @property {IconVariant['pixelDensity'] } pixelDensity
9+ * @property {Extract< IconVariant, {mimeType: 'image/png'}> ['pixelDensity'] } pixelDensity
1010 * @property {IconVariant['size'] } size
1111 *
1212 * @typedef {Object } SvgOpts
@@ -58,17 +58,7 @@ export class IconApi {
5858 const savedVariants = await Promise . all (
5959 icon . variants . map ( async ( { blob, ...variant } ) => {
6060 const blobVersionId = await this . #dataStore. writeRaw ( blob )
61-
62- return {
63- ...variant ,
64- blobVersionId,
65- pixelDensity :
66- // Pixel density does not apply to svg variants
67- // TODO: Ideally @mapeo /schema wouldn't require pixelDensity when the mime type is svg
68- variant . mimeType === 'image/svg+xml'
69- ? /** @type {const } */ ( 1 )
70- : variant . pixelDensity ,
71- }
61+ return { ...variant , blobVersionId }
7262 } )
7363 )
7464
@@ -146,7 +136,7 @@ const SIZE_AS_NUMERIC = {
146136 * 2. Matching size. If no exact match:
147137 * 1. If smaller ones exist, prefer closest smaller size.
148138 * 2. Otherwise prefer closest larger size.
149- * 3. Matching pixel density. If no exact match:
139+ * 3. Matching pixel density (when asking for PNGs) . If no exact match:
150140 * 1. If smaller ones exist, prefer closest smaller density.
151141 * 2. Otherwise prefer closest larger density.
152142 *
@@ -155,10 +145,11 @@ const SIZE_AS_NUMERIC = {
155145 */
156146export function getBestVariant ( variants , opts ) {
157147 const { size : wantedSize , mimeType : wantedMimeType } = opts
158- // Pixel density doesn't matter for svg so default to 1
159- const wantedPixelDensity =
160- opts . mimeType === 'image/svg+xml' ? 1 : opts . pixelDensity
161-
148+ /** @type {BitmapOpts['pixelDensity'] } */
149+ let wantedPixelDensity
150+ if ( opts . mimeType === 'image/png' ) {
151+ wantedPixelDensity = opts . pixelDensity
152+ }
162153 if ( variants . length === 0 ) {
163154 throw new Error ( 'No variants exist' )
164155 }
@@ -170,7 +161,6 @@ export function getBestVariant(variants, opts) {
170161 `No variants with desired mime type ${ wantedMimeType } exist`
171162 )
172163 }
173-
174164 const wantedSizeNum = SIZE_AS_NUMERIC [ wantedSize ]
175165
176166 // Sort the relevant variants based on the desired size and pixel density, using the rules of the preference.
@@ -182,18 +172,23 @@ export function getBestVariant(variants, opts) {
182172 const aSizeDiff = aSizeNum - wantedSizeNum
183173 const bSizeDiff = bSizeNum - wantedSizeNum
184174
185- // Both variants match desired size, use pixel density to determine preferred match
175+ // Both variants match desired size, use pixel density (when png) to determine preferred match
186176 if ( aSizeDiff === 0 && bSizeDiff === 0 ) {
187- // Pixel density doesn't matter for svg but prefer lower for consistent results
188- if ( opts . mimeType === 'image/svg+xml' ) {
189- return a . pixelDensity <= b . pixelDensity ? - 1 : 1
177+ // What to do if asking for an svg and both (a and b) are svgs and have the same size, what criteria do we use?
178+ // For now, we don't change sort order
179+ if ( wantedMimeType === 'image/svg+xml' ) {
180+ return 0
181+ } else if (
182+ wantedMimeType === 'image/png' &&
183+ a . mimeType === 'image/png' &&
184+ b . mimeType === 'image/png'
185+ ) {
186+ return determineSortValue (
187+ wantedPixelDensity ,
188+ a . pixelDensity ,
189+ b . pixelDensity
190+ )
190191 }
191-
192- return determineSortValue (
193- wantedPixelDensity ,
194- a . pixelDensity ,
195- b . pixelDensity
196- )
197192 }
198193
199194 return determineSortValue ( wantedSizeNum , aSizeNum , bSizeNum )
0 commit comments