mirror of
https://github.com/maputnik/editor.git
synced 2026-11-30 13:54:20 +00:00
# fix: improve previews for small sprites ## Description Small sprites appeared at their original display size in the sprite picker, making them difficult to recognize when choosing an icon or pattern. - Render previews in a fixed 36 × 36 CSS pixel canvas. Scale and center each sprite while preserving its aspect ratio. - Prefer matching `@2x.json` metadata and `@2x.png` atlas for previews. Use the base atlas when `@2x` metadata is unavailable. - Size the canvas backing store for the display's device pixel ratio. Preserve query strings and fragments when building sprite URLs. Follow-up to [#2364](https://github.com/maplibre/maputnik/pull/2364), which extended sprite previews to all sprite-backed fields as part of [#1294](https://github.com/maplibre/maputnik/issues/1294). ## Visuals ### Before Small sprites appear at their original display size in the picker.  ### After The same sprites are scaled to fit the fixed-size preview using the `@2x` atlas.  ## Verification All checks for this change passed. - Playwright covers the `@2x` atlas, the centered 36 × 36 preview for a small rectangular sprite, the 72 × 72 canvas at device pixel ratio 2, and the base atlas fallback. Existing coverage exercises SDF, sprite selection, image failures, and catalog updates. - `npm run lint` - `npm run lint-css` - `npm run build` - `npm run test-unit -- --run` (55 passed) - `npm run test -- e2e/sprite-picker.spec.ts --workers=2` (21 passed) - Confirmed that deliberately changing the expected CSS width, backing canvas width, and fallback pixel color fails the corresponding tests. Restored the correct assertions. ## Launch Checklist - [x] Briefly describe the changes in this PR. - [x] Link to related issues. - [x] Include before/after visuals or GIFs if this PR includes visual changes. - [x] Write tests for all new functionality. - [x] Add an entry to `CHANGELOG.md` under the `## main` section. Co-authored-by: FantasticSPM <asd507172@gmail.com>
61 lines
2.4 KiB
TypeScript
61 lines
2.4 KiB
TypeScript
import type { SpriteSpecification } from "@maplibre/maplibre-gl-style-spec";
|
|
import { spriteSources, spriteURL, type Sprite } from "./sprites";
|
|
|
|
async function loadJSON<T>(url: string, defaultValue: T): Promise<T> {
|
|
try {
|
|
const response = await fetch(url, {
|
|
mode: "cors",
|
|
credentials: "same-origin"
|
|
});
|
|
if (!response.ok) {
|
|
throw new Error("Failed to load metadata for " + url);
|
|
}
|
|
return await response.json();
|
|
} catch {
|
|
console.warn("Can not load metadata for " + url + ", using default value " + defaultValue);
|
|
return defaultValue;
|
|
}
|
|
}
|
|
|
|
export async function downloadGlyphsMetadata(urlTemplate: string): Promise<string[]> {
|
|
if(!urlTemplate) return [];
|
|
|
|
// Special handling because Tileserver GL serves the fontstacks metadata differently
|
|
// https://github.com/klokantech/tileserver-gl/pull/104#issuecomment-274444087
|
|
const urlObj = new URL(urlTemplate);
|
|
const normPathPart = "/" + encodeURIComponent("{fontstack}") + "/" + encodeURIComponent("{range}") + ".pbf";
|
|
if(urlObj.pathname === normPathPart) {
|
|
urlObj.pathname = "/fontstacks.json";
|
|
} else {
|
|
urlObj.pathname = urlObj.pathname!.replace(normPathPart, ".json");
|
|
}
|
|
const url = urlObj.toString();
|
|
const fonts = await loadJSON(url, [] as string[]);
|
|
return [...new Set(fonts)];
|
|
}
|
|
|
|
export async function downloadSpriteMetadata(sprite: SpriteSpecification | undefined): Promise<Sprite[]> {
|
|
const catalogs = await Promise.all(spriteSources(sprite).map(async ({ id, url }) => {
|
|
if (!url) return [];
|
|
try {
|
|
for (const [metadataExtension, imageExtension] of [["@2x.json", "@2x.png"], [".json", ".png"]] as const) {
|
|
const metadata = await loadJSON<Record<string, Omit<Sprite, "id" | "imageUrl">> | null>(
|
|
spriteURL(url, metadataExtension), null,
|
|
);
|
|
const entries = Object.entries(metadata ?? {}).filter(([, item]) => item &&
|
|
[item.x, item.y, item.width, item.height, item.pixelRatio].every(Number.isFinite) &&
|
|
item.x >= 0 && item.y >= 0 && item.width > 0 && item.height > 0 && item.pixelRatio > 0
|
|
);
|
|
if (entries.length) {
|
|
const imageUrl = spriteURL(url, imageExtension);
|
|
return entries.map(([name, item]) => ({ ...item, id: id === "default" ? name : `${id}:${name}`, imageUrl }));
|
|
}
|
|
}
|
|
} catch {
|
|
return [];
|
|
}
|
|
return [];
|
|
}));
|
|
return catalogs.flat();
|
|
}
|