Esempio n. 1
0
    addGlyph(id, name, glyph, buffer) {
        if (!glyph) return null;

        const key = `${name}#${glyph.id}`;

        // The glyph is already in this texture.
        if (this.index[key]) {
            if (this.ids[key].indexOf(id) < 0) {
                this.ids[key].push(id);
            }
            return this.index[key];
        }

        // The glyph bitmap has zero width.
        if (!glyph.bitmap) {
            return null;
        }

        const bufferedWidth = glyph.width + buffer * 2;
        const bufferedHeight = glyph.height + buffer * 2;

        // Add a 1px border around every image.
        const padding = 1;
        let packWidth = bufferedWidth + 2 * padding;
        let packHeight = bufferedHeight + 2 * padding;

        // Increase to next number divisible by 4, but at least 1.
        // This is so we can scale down the texture coordinates and pack them
        // into fewer bytes.
        packWidth += (4 - packWidth % 4);
        packHeight += (4 - packHeight % 4);

        let rect = this.bin.packOne(packWidth, packHeight);
        if (!rect) {
            this.resize();
            rect = this.bin.packOne(packWidth, packHeight);
        }
        if (!rect) {
            util.warnOnce('glyph bitmap overflow');
            return null;
        }

        this.index[key] = rect;
        this.ids[key] = [id];

        const target = this.data;
        const source = glyph.bitmap;
        for (let y = 0; y < bufferedHeight; y++) {
            const y1 = this.width * (rect.y + y + padding) + rect.x + padding;
            const y2 = bufferedWidth * y;
            for (let x = 0; x < bufferedWidth; x++) {
                target[y1 + x] = source[y2 + x];
            }
        }

        this.dirty = true;

        return rect;
    }
Esempio n. 2
0
    allocateImage(pixelWidth, pixelHeight) {
        pixelWidth = pixelWidth / this.pixelRatio;
        pixelHeight = pixelHeight / this.pixelRatio;

        // Increase to next number divisible by 4, but at least 1.
        // This is so we can scale down the texture coordinates and pack them
        // into 2 bytes rather than 4 bytes.
        // Pad icons to prevent them from polluting neighbours during linear interpolation
        const padding = 2;
        const packWidth = pixelWidth + padding + (4 - (pixelWidth + padding) % 4);
        const packHeight = pixelHeight + padding + (4 - (pixelHeight + padding) % 4);// + 4;

        const rect = this.bin.packOne(packWidth, packHeight);
        if (!rect) {
            util.warnOnce('SpriteAtlas out of space.');
            return null;
        }

        return rect;
    }