Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

possible to export bwipp_ functions #291

Closed
lublak opened this issue Apr 25, 2023 · 5 comments
Closed

possible to export bwipp_ functions #291

lublak opened this issue Apr 25, 2023 · 5 comments

Comments

@lublak
Copy link

lublak commented Apr 25, 2023

currently i can only use BwipJs export.
To improve the performance of the size caulculation (#281) i need to write my own BWIPJs Object and call bwipp_encode.
It woould be usefull to get an: BwipJs.bwipp.bwipp_encode etc.

@metafloor
Copy link
Owner

I am not sure what you are asking. Please provide a code sketch of what you would like to do.

@lublak
Copy link
Author

lublak commented Apr 26, 2023

@metafloor
currently i created somthing like this so i can auto scale a barcode.
But it would be faster if i create my own BwipJs class.
So i can remove all code which is not needed to calculate the size.

class CalculateSize implements DrawingOption<{width:number, height:number}> {
    width:number = 0;
    height:number = 0;
    constructor() {

    }
    scale(sx: number, sy: number): [number, number] | null {
        return null;
    }
    measure(str: string, font: string, fwidth: number, fheight: number): { width: number; ascent: number; descent: number; } {
        width = fwidth|0;
        fheight = fheight|0;

        var fontid = FontLib.lookup(font);
        var width = 0;
        var ascent = 0;
        var descent = 0;
        for (var i = 0; i < str.length; i++) {
            var ch = str.charCodeAt(i);
            var glyph = FontLib.getpaths(fontid, ch, fwidth, fheight);
            if (!glyph) {
                continue;
            }
            ascent  = Math.max(ascent, glyph.ascent);
            descent = Math.max(descent, -glyph.descent);
            width  += glyph.advance;
        }
        return { width, ascent, descent };
    }
    init(width: number, height: number): void {
        this.width = width;
        this.height = height;
    }
    line(x0: number, y0: number, x1: number, y1: number, lw: number, rgb: string): void {
        
    }
    polygon(pts: [number, number][]): void {
        
    }
    hexagon(pts: [[number, number], [number, number], [number, number], [number, number], [number, number]]): void {
        
    }
    ellipse(x: number, y: number, rx: number, ry: number, ccw: boolean): void {
        
    }
    fill(rgb: string): void {
        
    }
    text(x: number, y: number, str: string, rgb: string, font: { name: string; width: number; height: number; dx: number; }): void {
        
    }
    end(): {width:number, height:number} {
        return {
            width: this.width,
            height: this.height,
        }
    }
}
 const size = BwipJs.render({
    bcid: data.type,
    text: data.data,
    includetext: !!text,
    alttext: text
 }, new CalculateSize());

const ratio = Math.floor(width / size.width);
BwipJs.toCanvas({
    bcid: data.type,
    text: data.data,
    includetext: !!text,
    alttext: text,
    scale: ration
})

@metafloor
Copy link
Owner

metafloor commented Apr 28, 2023

I am finishing up the version 4 release of bwip-js and its biggest change is the ability to pass in user-provided drawing objects to the named exports. This will allow tree-shaking with custom drawing, which the current version does not support. I believe that is what you are asking for here. Please correct me if I misunderstand.

But your request also shows the need to include a built-in drawing object that just returns size. I will add that to the new release.

@lublak
Copy link
Author

lublak commented Apr 28, 2023

@metafloor yes :) this is it! Thanks!

@metafloor
Copy link
Owner

Version 4 is just released and includes the ability to supply custom drawing objects to the named exports. See here for the announcement: #299

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants