2

I'm trying to generate an image that contains right-to-left text on my node.js server.

I'm currently using node-canvas(based on "Cairo"), but open for suggestions in case other libraries will do the trick.

To be clear:

  1. I need support to be able writing right-to-left text
  2. The actual fonts are not relevant for this question, neither the direction of the font. E.G: I don't mind writing the words backwards in my software as long as they will be displayed in the image from right to left. (Hebrew for example is not supported and if i write the word "תודה" it will be written "התוד" instead. This i can handle, the problem is the paragraph direction.)
TBE
  • 1,002
  • 1
  • 11
  • 32
  • I have no clue about node.js, but Pango would be the right library for the job – Uli Schlachter Dec 26 '15 at 12:41
  • Hi, did you managed to solve this issue? – user2951807 Mar 06 '16 at 15:28
  • 1
    @user2951807 In my Ubuntu server nothing needed to be made, node-canvas worked as expected with the Hebrew font i used, However on my local dev machine (Macbook Pro) the letters are written backwards. Since i didn't want to waist anymore time on that issue, i just reversed the strings containing Hebrew letters before drawing them with node-canvas – TBE Mar 06 '16 at 16:10
  • i hoped it wont be the answer :( guess ill need to reverse it too. thanks! – user2951807 Mar 06 '16 at 16:19
  • @user2951807 I added an answer with smart reverse to ease things up for you – TBE Mar 07 '16 at 11:12
  • have you known what was the different between your local system and your server? – fingerpich Nov 07 '17 at 07:40
  • @fingerpich i never figure it out, its a waist of time. My guess is that it somehow connected to the fonts installed on the machine. In any case I'm no longer use these methods of work. I'm now creating all images by converting HTML to JPG with Pageres (npm name is "pageres") – TBE Nov 09 '17 at 10:58

1 Answers1

2

Here is a go around that fixes the problem:

smartReverse: function(str) {                     // Reverse a string considering english letters and and numbers.
    return str                                    // by taking it and
        .split(/\s+/)                            // splitting it into words
        .reverse()                               // and reversing the word order
        .map(function(word) {
            // and then changing each word
            return (/^[0-9\.]+$/.test(word) || /^[a-zA-Z0-9?><;,{}[\]\-_+=!@#$%\^&*|']+$/.test(word)) ?     // if it's a number or an english word
                word :                              // into itself
                word.split("").reverse().join("");                  // or otherwise into its reverse
        })
        .join(" ")                                  // put it back together
        ;
}
TBE
  • 1,002
  • 1
  • 11
  • 32
  • Thanks for the try but it not enough. The function is very basic and not helping when the str is mixed with rtl words and ltr words in the same sentance or even brackets or new lines.. The solution we need is one that can get a textarea output and wich can be anything including math formula's and reverse it perfectly when needed. – user2951807 Mar 13 '16 at 09:07