-
Notifications
You must be signed in to change notification settings - Fork 310
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
Comments
I am not sure what you are asking. Please provide a code sketch of what you would like to do. |
@metafloor 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
}) |
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. |
@metafloor yes :) this is it! Thanks! |
Version 4 is just released and includes the ability to supply custom drawing objects to the named exports. See here for the announcement: #299 |
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.The text was updated successfully, but these errors were encountered: