sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    WebDialog and utf-8 Problem

    Scheduled Pinned Locked Moved Developers' Forum
    5 Posts 3 Posters 324 Views 3 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • B Offline
      bluetale
      last edited by

      Hello!

      I've problems with the transfer of Unicode-characters from a WebDialog back to ruby.
      The WebDialog-charset is utf-8.

      For example, I write the text "tető" into a inputbox and I'd like to transfer it to ruby.
      I write:

      window.location="skp;export_command@" + text;
      

      Ok that works. In Sketchup I get the text with

      wd.add_action_callback("export_command") do |wd, args| ... 
      

      Now, my problem is that the text in 'args' is different than the originally one.
      Instead of

      "tető".unpack('U*')
      [116, 101, 116, 337]
      

      I get: [116, 101, 116, 111]

      Is there a way to handle this correctly? My very dirty solution is to write the utf-8 value for each character via javascript into a string, e.g. "[116, 101, 116, 337]" and then send this sequence back to ruby. After that I build an array and make

      array.pack('U*')
      

      to get the correct text.

      1 Reply Last reply Reply Quote 0
      • thomthomT Offline
        thomthom
        last edited by

        Are you sure you process everything as UTF-8 all the way through?

        ASCII Byte 111 is "o". Something is lost here at some point.

        As long as your HTML and Javascript is both in UTF-8 it should work fine. I've so far not seen this behaviour.

        You use "U*" - but I find it easier to inspect the actual bytes "C*".

        Where are your string "tető" originating from? From the ruby script? If so, is that by any chance encoded in ANSI?
        I wonder if you feed the webdialog an ANSI string, which has different bytes for the characters outside the ASCII range which then get jumbled up in the Webdialoge.

        When you try in the Ruby Console the characters are UTF-8 encoded and therefore works fine, but if you then enter that same code into an ANSI encoded file you can easily get odd output.

        Usually when you get unexpected characters it's some part of the string handling chain that isn't UTF-8. While making translation system for my last plugin I ran into issues where it turned out that one of the language files wasn't in UTF-8 and that caused havoc for everything else.

        Thomas Thomassen — SketchUp Monkey & Coding addict
        List of my plugins and link to the CookieWare fund

        1 Reply Last reply Reply Quote 0
        • T Offline
          todd burch
          last edited by

          To successfully transfer UTF-8 (or any encoding) back and forth from Ruby to a WebDialog, the easiest approach is to convert your characters to printable hex in javascript and pass that back. Then, in Ruby, pack it. Works great. If you search this forum, I believe you'll find some examples.

          Todd

          1 Reply Last reply Reply Quote 0
          • thomthomT Offline
            thomthom
            last edited by

            When you do a callback from a webdialoge to ruby it is done by a URI string - which means that the data in the URI needs to be URI encoded. Quite possibly your character is converted here. Since that you have a 2048 character limit for URI's under Explorer I've been using hidden input elements to transfer data.

            So instead of a callback with arguments, I do:

            1. Put the data I want transferred into an hidden input element.
            2. Do a callback to ruby.
            3. Receiving ruby method reads data from hidden input.

            That should preserve your characters,
            let you avoid escaping data in the URI string,
            let you use any length of data.

            Thomas Thomassen — SketchUp Monkey & Coding addict
            List of my plugins and link to the CookieWare fund

            1 Reply Last reply Reply Quote 0
            • B Offline
              bluetale
              last edited by

              thanks for your advice, I will try your suggested methods.

              1 Reply Last reply Reply Quote 0
              • 1 / 1
              • First post
                Last post
              Buy SketchPlus
              Buy SUbD
              Buy WrapR
              Buy eBook
              Buy Modelur
              Buy Vertex Tools
              Buy SketchCuisine
              Buy FormFonts

              Advertisement