huysentruitw / barcoder

Lightweight Barcode Encoding Library for .NET Framework, .NET Standard and .NET Core.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unicode in QR

gmjn opened this issue · comments

In QR code, my Norwegian characters are wrong (æøåÆØÅ). I checked the code in the barcoder/src/Barcoder/Qr/InternalEncoders/UnicodeEncoder.cs file and found the following error.

  1. Byte order mark is missing.
  2. When data length is set, content length is used and not data length.

I corrected this in the code and now my Norwegian letters work fine. Here is my code now:

internal sealed class UnicodeEncoder : InternalEncoderBase
{
    public override (BitList, VersionInfo) Encode(string content, ErrorCorrectionLevel errorCorrectionLevel)
    {
        bool insertBom = false; // Insert Byte order mark (BOM) for UTF-8

        if (content == null) throw new ArgumentNullException(nameof(content));

        foreach (char item in content)
        {
            if (item > 127) // Check if BOM is needed
            {
                insertBom = true;
                break;
            }
        }
        
        byte[] data = System.Text.Encoding.UTF8.GetBytes(content);
        int dataLength = insertBom ? data.Length + 3 : data.Length;

        EncodingMode encodingMode = EncodingMode.Byte;
        var versionInfo = VersionInfo.FindSmallestVersionInfo(errorCorrectionLevel, encodingMode, dataLength * 8);
        if (versionInfo == null)
            throw new InvalidOperationException("Too much data to encode");

        var bits = new BitList();
        bits.AddBits((uint)encodingMode, 4);
        bits.AddBits((uint)dataLength, versionInfo.CharCountBits(encodingMode));

        if (insertBom) // Insert BOM
        {
            bits.AddByte(239);
            bits.AddByte(187);
            bits.AddByte(191);
        }

        foreach (var b in data)
            bits.AddByte(b);
        AddPaddingAndTerminator(ref bits, versionInfo);
        return (bits, versionInfo);
    }
}

Thanks for raising this issue. Are you willing to create a unit-test with fix as a pull-request or do you want me to take care of it?