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

Barcode Text not rendered in Viewer output #119

Closed
primo-ppcg opened this issue Jun 22, 2022 · 12 comments
Closed

Barcode Text not rendered in Viewer output #119

primo-ppcg opened this issue Jun 22, 2022 · 12 comments
Assignees

Comments

@primo-ppcg
Copy link
Collaborator

Using:

.NET Framework 4.7.2
BinaryKits.Zpl.Label 3.1.0
BinaryKits.Zpl.Viewer 1.1.1

All other dependencies up to date.

Method for creating label preview:

DialogResult result = this.openFileDialog.ShowDialog();
if(result == DialogResult.OK) {
    string content = File.ReadAllText(this.openFileDialog.FileName);

    IPrinterStorage printerStorage = new PrinterStorage();
    ZplElementDrawer drawer = new ZplElementDrawer(printerStorage);

    ZplAnalyzer analyzer = new ZplAnalyzer(printerStorage);
    AnalyzeInfo analyzeInfo = analyzer.Analyze(content);

    foreach(LabelInfo labelInfo in analyzeInfo.LabelInfos) {
        byte[] labelData = drawer.Draw(labelInfo.ZplElements);
        File.WriteAllBytes("label.png", labelData);
    }
}

Using this sample label data:

^XA

^BY3,2,100
^FO100,750,^BC^FDTEST0123456789^FS

^XZ

Actual:

Expected (labelary.com reference):

@tinohager
Copy link
Collaborator

Hello this is a known bug that we still need to fix

@primo-ppcg
Copy link
Collaborator Author

primo-ppcg commented Jun 22, 2022

@tinohager thanks for the quick reply.

I looks like IncludeLabel isn't being specified in the various BarcodeElementDrawers:

var barcodeElement = new Barcode
{
    BarWidth = barcode.ModuleWidth,
    BackColor = Color.Transparent,
    Height = barcode.Height
};

For the example above, I was able to get fairly reasonable results using the following:

var barcodeElement = new Barcode {
    BarWidth = barcode.ModuleWidth,
    BackColor = Color.Transparent,
    Height = barcode.Height + 33,
    IncludeLabel = true,
    LabelFont = new Font("DejaVu Sans Mono", 22)
};

The appropriate label size and additional height would need to computed at some point before the draw, but I don't see a convenient site. Likely these would need to be passed as properties of the ZplBarcode.

@tinohager
Copy link
Collaborator

The problem is that the label can also be above the barcode

^XA

^FX Third section with bar code.
^BY5,2,270
^FO100,550
^B1N,N,158,Y,Y
^FD12345678^FS

^XZ

@ingfdoaguirre
Copy link
Contributor

ingfdoaguirre commented Jun 22, 2022

you need to get the text width and define offset in X and Y depending on the case:

//Define offset in X and Y
offsetX := x + (barcodeWidth-textWidth)/2
offsetY := y + barcodeHeight

//If text needed to be above barcode, recalculate offsets
if PrintInterpretationLineAboveCode {
	offsetX = x + (barcodeWidth-textWidth)/2
	offsetY = y - fontHeight
}

This is a pseudocode, but It will work, I have ported the viewer to Go and draw the text correctly

@tinohager
Copy link
Collaborator

@ingfdoaguirre is your viewer also open source available? Is there a reason for porting to another language?

@ingfdoaguirre
Copy link
Contributor

@ingfdoaguirre is your viewer also open source available? Is there a reason for porting to another language?

Hi Tino, first I'm dont have the sufficient knowledge of C#, also I used tried to use the viewer to create PDF files intead images.

The first approach was to only append the image to a pdf file, but the final file size, was big on a document with some labels.

Then I tried pdfSharp to make "native pdf file", it worked well.

The problem came when I made some tests, the ram memory usage was increasingly, lets say that on every page added to document, ram was increased by 20mb. I tried some some solutions, but no one worked. Then I tried on my win11 partition, and the ram leak was not present (I develop on Ubuntu).

After some research I discovered that the "System.Drawing.Common" library only works on windows (: and the problem with memory leak, was pdfSharp that doesn't already implemented a solution to that.

Also this is the only open source library to analyze Zpl, and I use Go in my microservices, so I made a port.

I will upload that port to github and make open source this week, I'm solving some details. And I will explicitily say that is a port from this library, thats the reality and not a problem to me.

Also was a personal challenge, and it was fun.

@tinohager
Copy link
Collaborator

tinohager commented Jun 22, 2022

Have you already built your port on the Protocol project?
Viewer based on Label instead of Protocol

@primo-ppcg
Copy link
Collaborator Author

The problem is that the label can also be above the barcode

You can also set the LabelPosition of the BarcodeLib.Barcode to TOPCENTER, e.g.:

var barcodeElement = new Barcode {
    BarWidth = barcode.ModuleWidth,
    BackColor = Color.Transparent,
    Height = barcode.Height + barcode.LabelHeight,
    IncludeLabel = true,
    LabelPosition = LabelPositions.TOPCENTER,
    LabelFont = barcode.LabelFont
};

@tinohager
Copy link
Collaborator

@primo-ppcg Can you create a pull request?

@primo-ppcg
Copy link
Collaborator Author

@primo-ppcg Can you create a pull request?

The one bit of difficulty that I'm having is that BarcodeLib takes a System.Drawing.Font rather than a SKFont. I'm not quite sure how to deal with that in an elegant way.

@YipingRuan
Copy link
Contributor

@primo-ppcg
Is this fixed by #122 ?

@primo-ppcg
Copy link
Collaborator Author

@primo-ppcg Is this fixed by #122 ?

Mode A is complete, mode N is partially supported. I intend to add support for mode D, and more complete support for mode N - as much as can be with the BarcodeLib library.

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

4 participants