infoxicator / react-native-star-prnt

React-Native bridge to communicate with Star Micronics Bluetooth/LAN Printers
MIT License
67 stars 65 forks source link

Cyrillic encoding seems not to work #69

Open vasilevzhivko opened 3 years ago

vasilevzhivko commented 3 years ago

Hello, I am trying to print in Bulgarian language, but the encoding seems not to work. Here is my code:

var commandsArray = [];
commandsArray.push({appendEncoding: StarPRNT.Encoding.Windows1251});
commandsArray.push({appendAlignment: StarPRNT.AlignmentPosition.Center});
commandsArray.push({append: '* ******** *\n'});
commandsArray.push({append: '* Български *\n'});
commandsArray.push({
  appendCutPaper: StarPRNT.CutPaperAction.PartialCutWithFeed,
});

async function print() {
  try {
    var printResult = await StarPRNT.print(
      'StarPRNT',
      commandsArray,
      'BT:00:15:0E:E6:6E:74',
    );
    console.log(printResult); // Success!
  } catch (e) {
    console.error(e);
  }
}

when I print I only get some strange symbols. Anyone else tried to print in Bulgarian/Russian?

infoxicator commented 3 years ago

Take a look at the official Star documentation to see if you can find the correct encoding

vasilevzhivko commented 3 years ago

Take a look at the official Star documentation to see if you can find the correct encoding

Found the solution, if you want I can create a pull request. Its quite simple actually for some printers (for example mine: SM-S230I) the encoding is really specific in my case for Bulgarian language is CP855 and for Russian is CP866. So what is needed is to add the encodings in types\index.d.ts like so:

    static Encoding: {
        USASCII: string,
        Windows1252: string,
        ShiftJIS: string,
        Windows1251: string,
        GB2312: string,
        Big5: string,
        UTF8: string,
        CP855: string
    };

and also in RNStarPrntModule.java a little modification to the getEncoding function like so:

        else if (encoding.equals("CP855")) {
            try {
                return Charset.forName("CP855"); // Bulgarian
            }
            catch (UnsupportedCharsetException e) { //not supported using UTF-8 Instead
                return Charset.forName("UTF-8");
            }
        }

p.s. I found the encodings here: Encodings for Java