Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #23940

Re: I try to write out sigma sign in javascript on a canvas, but HTML encoding do not work, how should i do?

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: I try to write out sigma sign in javascript on a canvas, but HTML encoding do not work, how should i do?
Date 2014-04-25 12:42 +0200
Organization PointedEars Software (PES)
Message-ID <2732090.DMPj7CqQq3@PointedEars.de> (permalink)
References <9521275e-7eb0-4d31-adc0-76044b3d33e1@googlegroups.com> <535a21aa$0$6703$9b4e6d93@newsspool2.arcor-online.net>

Show all headers | View raw


Martin Honnen wrote:

> jonas.thornvall@gmail.com wrote:
>> I've tried to assign string func both with and without enclosing
>> brackets. But it just write out the ASCII not the encoding on canvas. So
>> HTML encode chars do not work on canvas. How can i solve this?
>>
>> //Not working arr[j].func = ("&sum;(1 &le; n &lt;"+n+")n?");
>> //Not working arr[j].func = "&sum;(1 &le; n &lt;"+n+")n?";
> 
> You should be able to use any Unicode character literally in your script
> code as long as you use an Unicode encoding like UTF-8 for your script
> files e.g. so use
> 
>    arr[j].func = "∑(1 ≤ n <100)n?";

[I would type those using AltGr+Shift+S and Shift+AltGr, “<”, “=” :)]

True.  However, two things one needs to be aware of:

  1. You also need to serve those files declaring the used character
     encoding in the Content-Type header field value because ECMAScript
     has no way of telling the parser the used character encoding yet,
     and there is no default encoding ubiquitously implemented.

     A possibility is to add

       <IfModule mime_module>
         AddType text/javascript;charset=utf-8 .js
       </IfModule>

     to your .htaccess file if you are using an httpd-compatible
     server like Apache.

  2. Without a BOM, a text editor cannot tell UTF-8 encoding from
     a one-byte encoding like ISO-8859-1.  One can support UTF-8-capable
     editors by tagging source code.  For example, for Vim you can insert
     (at the beginning or the end; I recommend the latter to preserve line
     numbers)
     
       /* vim:set fileencoding=utf-8 */

     which removes the need to issue the command “:set encoding=utf-8”
     manually or use “set encoding=utf-8” in one’s vimrc.

     Although all my source code is UTF-8 encoded by default (Eclipse
     preference), I have begun to tag it like that when I use characters
     whose code point is beyond U+007F (characters not in US-ASCII), for
     example in JSX:math/complex.js [1]

If using a Unicode encoding, thus using Unicode characters verbatim, is not 
an option for you, ECMAScript supports Unicode escape sequences [2]:

  arr[j].func = "\u2211(1 \u2264 n <100)n?";  // ¹

Constants or constant-like variables or properties can be used to make such 
code easier readable, especially if symbols are used repeatedly:

  /* you may use “const” if your target implementations support it */
  var CH_SUM = "\u2211";
  var CH_LTE = "\u2264";

  arr[j].func = CH_SUM + "(1 " + CH_LTE + " n <100)n?";

One should be aware, however, that using escape sequences instead of plain 
characters increases the file size (here: 6 or more instead of up to four
8-bit bytes), and requires more parsing, therefore increases the time it 
takes to load a document.  That applies both to ECMAScript and markup 
documents, like HTML.

If you are using HTML5 or XHTML, consider using MathML.  Gecko-based 
browsers support MathML well [3]; recently a user stylesheet has been 
proposed for working around Chromium’s currently disabled MathML 
support. [4]

_______
¹  I have obtained this using

     "∑(1 ≤ n <100)n?".split("").map(function (char) {
       if (char.charCodeAt(0) > 0x7F)
       {
         return "\\u" + char.charCodeAt(0).toString(16);
       }
       else
       {
         return char;
       }
     }).join("")

   (as a one-liner with another identifier) on the script console.  It can
   be improved to give the correct escape sequence for characters below
   U+1000, too.  See also <http://mothereff.in/js-escapes> by Mathias Bynens
   (@mathias).


[1] 
<http://PointedEars.de/wsvn/JSX?op=comp&compare[]=/trunk/@533&compare[]=/trunk/@534>
[2] <http://ecma-international.org/ecma-262/5.1/#sec-7.8.4>
[3] Testcases:
    <http://pointedears.de/markup/equation2.html>
    <http://pointedears.de/markup/equation2.xhtml>
    <http://pointedears.de/markup/xml/SVG/doppler>
[4] <https://code.google.com/p/chromium/issues/detail?id=152430#c94>
-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

I try to write out sigma sign in javascript on a canvas, but HTML encoding do not work, how should i do? jonas.thornvall@gmail.com - 2014-04-25 01:43 -0700
  Re: I try to write out sigma sign in javascript on a canvas, but HTML encoding do not work, how should i do? Martin Honnen <mahotrash@yahoo.de> - 2014-04-25 10:49 +0200
    Re: I try to write out sigma sign in javascript on a canvas, but HTML encoding do not work, how should i do? jonas.thornvall@gmail.com - 2014-04-25 02:30 -0700
    Re: I try to write out sigma sign in javascript on a canvas, but HTML encoding do not work, how should i do? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-04-25 12:42 +0200
      Re: I try to write out sigma sign in javascript on a canvas, but HTML encoding do not work, how should i do? jonas.thornvall@gmail.com - 2014-04-25 21:26 -0700
      Re: I try to write out sigma sign in javascript on a canvas, but HTML encoding do not work, how should i do? jonas.thornvall@gmail.com - 2014-04-25 21:38 -0700
      Re: I try to write out sigma sign in javascript on a canvas, but HTML encoding do not work, how should i do? jonas.thornvall@gmail.com - 2014-04-25 21:40 -0700
      Re: I try to write out sigma sign in javascript on a canvas, but HTML encoding do not work, how should i do? Richard Damon <Richard@Damon-Family.org> - 2014-04-26 11:02 -0400
        Re: I try to write out sigma sign in javascript on a canvas, but HTML encoding do not work, how should i do? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-04-26 18:20 +0200

csiph-web