Skip to content

Commit

Permalink
Merge branch 'dev' into next
Browse files Browse the repository at this point in the history
  • Loading branch information
bigtimebuddy committed Aug 25, 2022
2 parents 0b4e269 + cdec607 commit e169d5b
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 11 deletions.
3 changes: 2 additions & 1 deletion bundles/pixi.js-node/src/adapter/loadNodeBitmapFont.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ async function _loadBitmap(src: string, data: BitmapFontData, loader: Loader)
textureUrls.push(url);
}

const textures: Texture[] = Object.values(await loader.load(textureUrls));
const loadedTextures = await loader.load(textureUrls) as Record<string, Texture>;
const textures = textureUrls.map((url) => loadedTextures[url]);

return BitmapFont.install(data, textures, true);
}
Expand Down
3 changes: 2 additions & 1 deletion packages/assets/src/loader/parsers/loadBitmapFont.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ export const loadBitmapFont = {
textureUrls.push(imagePath);
}

const textures: Texture[] = Object.values(await loader.load(textureUrls));
const loadedTextures = await loader.load(textureUrls) as Record<string, Texture>;
const textures = textureUrls.map((url) => loadedTextures[url]);

return BitmapFont.install(fontData, textures, true);
},
Expand Down
16 changes: 16 additions & 0 deletions packages/assets/test/assets/bitmap-font/split_font2.fnt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<font>
<info face="split_font2" size="24" bold="0" italic="0" charset="" unicode="" stretchH="100" smooth="1" aa="1" padding="2,2,2,2" spacing="0,0" outline="0"/>
<common lineHeight="27" base="18" scaleW="22" scaleH="46" pages="2" packed="0"/>
<pages>
<page id="1" file="split_font_cd.png"/>
<page id="0" file="split_font_ab.png"/>
</pages>
<chars count="5">
<char id="65" x="2" y="2" width="19" height="20" xoffset="0" yoffset="0" xadvance="16" page="0" chnl="15"/>
<char id="66" x="2" y="24" width="15" height="20" xoffset="2" yoffset="0" xadvance="16" page="0" chnl="15"/>

<char id="67" x="2" y="2" width="18" height="20" xoffset="1" yoffset="0" xadvance="17" page="1" chnl="15"/>
<char id="68" x="2" y="24" width="17" height="20" xoffset="2" yoffset="0" xadvance="17" page="1" chnl="15"/>
</chars>
<kernings count="0"/>
</font>
20 changes: 20 additions & 0 deletions packages/assets/test/loader.tests.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ImageResource } from '@pixi/core';
import { Texture } from '@pixi/core';
import type { Spritesheet } from '@pixi/spritesheet';
import { BitmapFont } from '@pixi/text-bitmap';
Expand Down Expand Up @@ -369,4 +370,23 @@ describe('Loader', () =>

expect(foundFont).toBe(false);
});

it('should split fonts if page IDs are in chronological order', async () =>
{
const loader = new Loader();

loader['_parsers'].push(loadTextures, loadBitmapFont);

const font = await loader.load(`${serverPath}bitmap-font/split_font2.fnt`);

const charA = font.chars['A'.charCodeAt(0)];
const charC = font.chars['C'.charCodeAt(0)];
const charATexture = charA.texture as Texture<ImageResource>;
const charCTexture = charC.texture as Texture<ImageResource>;

expect(charA.page).toEqual(0);
expect(charC.page).toEqual(1);
expect(charATexture.baseTexture.resource.src).toEqual(`${serverPath}bitmap-font/split_font_ab.png`);
expect(charCTexture.baseTexture.resource.src).toEqual(`${serverPath}bitmap-font/split_font_cd.png`);
});
});
25 changes: 22 additions & 3 deletions packages/utils/src/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ function assertPath(path: string)
}
}

function removeUrlParams(url: string): string
{
const re = url.split('?')[0];

return re.split('#')[0];
}

function escapeRegExp(string: string)
{
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
Expand Down Expand Up @@ -209,8 +216,8 @@ export const path: Path = {
{
if (this.isDataUrl(url)) return url;

const baseUrl = this.toPosix(customBaseUrl ?? settings.ADAPTER.getBaseUrl());
const rootUrl = this.toPosix(customRootUrl ?? this.rootname(baseUrl));
const baseUrl = removeUrlParams(this.toPosix(customBaseUrl ?? settings.ADAPTER.getBaseUrl()));
const rootUrl = removeUrlParams(this.toPosix(customRootUrl ?? this.rootname(baseUrl)));

assertPath(url);
url = this.toPosix(url);
Expand Down Expand Up @@ -291,7 +298,19 @@ export const path: Path = {
if (arg.length > 0)
{
if (joined === undefined) joined = arg;
else joined += `/${arg}`;
else
{
const prevArg = segments[i - 1] ?? '';

if (this.extname(prevArg))
{
joined += `/../${arg}`;
}
else
{
joined += `/${arg}`;
}
}
}
}
if (joined === undefined) { return '.'; }
Expand Down
49 changes: 43 additions & 6 deletions packages/utils/test/path.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ describe('Paths', () =>

it('should join paths', () =>
{
expect(path.join('http://foo.com/index.html', '../bar/baz/file')).toBe('http://foo.com/bar/baz/file');
expect(path.join('http://foo.com/bar/index.html', '../baz/file')).toBe('http://foo.com/baz/file');
expect(path.join('http://foo.com/bar/index.html', './baz/file')).toBe('http://foo.com/bar/baz/file');
expect(path.join('http://foo.com/bar/index.html', 'baz/file')).toBe('http://foo.com/bar/baz/file');
expect(path.join('http://foo.com/bar/index.html?var=a', '../baz/file')).toBe('http://foo.com/baz/file');
expect(path.join('http://foo.com/bar/index.html?var=a#hash', '../baz/file')).toBe('http://foo.com/baz/file');
expect(path.join('http://foo.com/bar/index.html#hash', '../baz/file')).toBe('http://foo.com/baz/file');

expect(path.join('http://foo.com', '../bar/baz/file')).toBe('http://foo.com/bar/baz/file');
expect(path.join('https://foo.com', '../bar/baz/file')).toBe('https://foo.com/bar/baz/file');
expect(path.join('file:///foo', '../bar/baz/file')).toBe('file:///bar/baz/file');
Expand Down Expand Up @@ -211,17 +219,46 @@ describe('Paths', () =>

it('should create absolute urls', () =>
{
expect(path.toAbsolute('browser.png', 'http://example.com/page-1/'))
.toEqual(`http://example.com/page-1/browser.png`);
// relative paths
expect(path.toAbsolute('./img/browser.png', 'http://example.com/page-1/'))
.toEqual(`http://example.com/page-1/img/browser.png`);
expect(path.toAbsolute('./img/browser.png', 'http://example.com/page-1/'))
.toEqual(`http://example.com/page-1/img/browser.png`);
expect(path.toAbsolute('browser.png', 'http://example.com/page-1')).toEqual(`http://example.com/page-1/browser.png`);
expect(path.toAbsolute('/browser.png', undefined, 'http://example.com/')).toEqual(`http://example.com/browser.png`);
expect(path.toAbsolute('/browser.png', undefined, 'http://example.com')).toEqual(`http://example.com/browser.png`);

expect(path.toAbsolute('img/browser.png', 'http://example.com/page-1/'))
.toEqual(`http://example.com/page-1/img/browser.png`);
expect(path.toAbsolute('windows.png', 'C:/foo/bar/')).toEqual(`C:/foo/bar/windows.png`);
expect(path.toAbsolute('windows.png', 'C:/foo/bar')).toEqual(`C:/foo/bar/windows.png`);
expect(path.toAbsolute('windows.png', 'C:\\foo\\bar\\')).toEqual(`C:/foo/bar/windows.png`);
expect(path.toAbsolute('mac.png', '/foo/bar/')).toEqual(`/foo/bar/mac.png`);
expect(path.toAbsolute('mac.png', '/foo/bar')).toEqual(`/foo/bar/mac.png`);

// paths with extensions
expect(path.toAbsolute('./browser.png', 'http://example.com/page-1/index.html'))
.toEqual(`http://example.com/page-1/browser.png`);
expect(path.toAbsolute('./img/browser.png', 'http://example.com/page-1/index.html'))
.toEqual(`http://example.com/page-1/img/browser.png`);
expect(path.toAbsolute('img/browser.png', 'http://example.com/page-1/index.html'))
.toEqual(`http://example.com/page-1/img/browser.png`);
expect(path.toAbsolute('windows.png', 'C:/foo/bar/index.html')).toEqual(`C:/foo/bar/windows.png`);
expect(path.toAbsolute('mac.png', '/foo/bar/index.html')).toEqual(`/foo/bar/mac.png`);

// path with query string
expect(path.toAbsolute('./browser.png', 'http://example.com/page-1/index.html?var=a#hash'))
.toEqual(`http://example.com/page-1/browser.png`);
expect(path.toAbsolute('./img/browser.png', 'http://example.com/page-1/index.html?var=a#hash'))
.toEqual(`http://example.com/page-1/img/browser.png`);
expect(path.toAbsolute('img/browser.png', 'http://example.com/page-1/index.html?var=a#hash'))
.toEqual(`http://example.com/page-1/img/browser.png`);
expect(path.toAbsolute('img/browser.png', 'http://example.com/page-1?var=a#hash'))
.toEqual(`http://example.com/page-1/img/browser.png`);
expect(path.toAbsolute('img/browser.png', 'http://example.com/page-1/?var=a#hash'))
.toEqual(`http://example.com/page-1/img/browser.png`);

// root relative paths
expect(path.toAbsolute('/browser.png', undefined, 'http://example.com/')).toEqual(`http://example.com/browser.png`);
expect(path.toAbsolute('/browser.png', undefined, 'http://example.com')).toEqual(`http://example.com/browser.png`);

expect(path.toAbsolute('/windows.png', undefined, 'C:/foo/')).toEqual(`C:/foo/windows.png`);
expect(path.toAbsolute('/windows.png', undefined, 'C:/foo')).toEqual(`C:/foo/windows.png`);
expect(path.toAbsolute('/windows.png', undefined, 'C:\\foo\\')).toEqual(`C:/foo/windows.png`);
Expand All @@ -234,7 +271,7 @@ describe('Paths', () =>
expect(path.toAbsolute('C:\\windows.png')).toEqual(`C:/windows.png`);
});

it.only('should detect if path is a data url', () =>
it('should detect if path is a data url', () =>
{
/* eslint-disable max-len */
const valid = [
Expand Down

0 comments on commit e169d5b

Please sign in to comment.