aruZeta / QRgen

A QR code generation library.
https://aruzeta.github.io/QRgen/
MIT License
103 stars 8 forks source link

Windows "cannot find encoding ISO 8859-1" #18

Closed EyeCon closed 2 years ago

EyeCon commented 2 years ago

As I was trying to run the example code given in the docs:

import QRgen
let urlQR = newQR("https://my-url.domain")
urlQR.printTerminal

...I get the error:

Error: unhandled exception: cannot find encoding ISO 8859-1 [EncodingError]
Error: execution of an external program failed: 'C:\Users\EyeCon\nimcache\qrgentest1_r\qrgentest1_99D5382AA8D22F6EB0B2F4D3D76FCB54F9ECD0A3.exe'

I am on Windows on Nim devel. I installed QRgen via nimble with nimble install qrgen.

aruZeta commented 2 years ago

Encoding iirc uses the OS to change the encoding of a string, in this case UTF-8 to ISO 8859-1, which is what the QR spec specifies to use when encoding byte mode strings. Seems like windows does not support that encoding? I will research about it later since today I won't be home sry.

aruZeta commented 2 years ago

Seems like Windows 1252 is the same as ISO 8859-1, I'm not sure, but if you can try it and find out it would be nice.

enthus1ast commented 2 years ago

For me it works when i change:

template encodeByteModeData(self: var EncodedQRCode, data: string) =
  ## Encodes `data` via the byte mode encoding algorithm.
  # for c in convert(data, "ISO 8859-1", "UTF-8"):
  for c in convert(data, "Windows-1252", "UTF-8"):
    self.data.add cast[uint8](c), 8
aruZeta commented 2 years ago

For me it works when i change:

template encodeByteModeData(self: var EncodedQRCode, data: string) =
  ## Encodes `data` via the byte mode encoding algorithm.
  # for c in convert(data, "ISO 8859-1", "UTF-8"):
  for c in convert(data, "Windows-1252", "UTF-8"):
    self.data.add cast[uint8](c), 8

Nice! Then the solution will be to just use a when block for windows with that code, will make the commit when I get home, or maybe you can do a PR (to develop).

enthus1ast commented 2 years ago

it seems that there is no "correct" way to encode stuff. I've google a little and saw that alot scanners use heuristic to find the correct encoding anyhow. I just disabled the (re)encoding in my branch, and my phone was able to read a string like "hallöäüд" correctly.

Some discussions i found: https://stackoverflow.com/questions/9699657/is-utf-8-the-encoding-of-choice-for-qr-codes-with-non-ascii-chars-by-now

To be honest, i would just use utf-8, or maybe just add an option to disable reencoding completely.

EyeCon commented 2 years ago

Please make UTF-8 the default. It is the most used one by the modern operating systems, and has the most coverage anyway.

aruZeta commented 2 years ago

it seems that there is no "correct" way to encode stuff. I've google a little and saw that alot scanners use heuristic to find the correct encoding anyhow. I just disabled the (re)encoding in my branch, and my phone was able to read a string like "hallöäüд" correctly.

Some discussions i found: https://stackoverflow.com/questions/9699657/is-utf-8-the-encoding-of-choice-for-qr-codes-with-non-ascii-chars-by-now

To be honest, i would just use utf-8, or maybe just add an option to disable reencoding completely.

Byte mode is supposed to work with UTF-8 too, the problem I read about is that not all scanners support UTF-8 in byte mode, but tbh I'm not sure and just followed what thonky's website said.

aruZeta commented 2 years ago

Hmm as that stack overflow says it might be better to just stick with UTF-8

aruZeta commented 2 years ago

Please make UTF-8 the default. It is the most used one by the modern operating systems, and has the most coverage anyway.

Yh, that's what seems we will do, and it's an easy fix by just removing the encode line.

aruZeta commented 2 years ago

The last commit should fix the issue, if there still are problems, reopen this issue.

aruZeta commented 2 years ago

Forgot to add: I'm releasing in a moment v1.0.1 with the fix.

EyeCon commented 2 years ago

Thanks! I'll try it when I can.

EyeCon commented 2 years ago

Version 1.0.1 fixes the issue. I tried with many characters outside the 8859-1 or cp1252 range and three different decoders recognized them with no problems.

Thank you for the library, I'll be using it a lot!

aruZeta commented 2 years ago

Nice! Thanks for bringing the issue!