5,693,062 members and growing! (21,422 online)
Email Password   helpLost your password?
Web Development » Client side scripting » General     Intermediate License: The MIT License

See your name in Braille

By Olivier Giulieri

Simple Javascript to display Braille in web pages using pictures
Javascript, XML, Windows, .NET, WebForms, Ajax, ASP.NET, Dev

Posted: 2 Apr 2007
Updated: 2 Apr 2007
Views: 10,489
Bookmarked: 5 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
7 votes for this Article.
Popularity: 3.23 Rating: 3.82 out of 5
1 vote, 14.3%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
1 vote, 14.3%
4
5 votes, 71.4%
5

Introduction

This article offers some JavaScript to display Braille in your web pages. It can be used to familiarize sighted people with the Braille system, not for real Braille transcription (that would be a much bigger project).

In its simplest form (Grade 1) Braille is just a different character set:

Screenshot - BrailleAlphabet.gif

Displaying Braille using pictures and JavaScript

From the table above, we can easily display a Braille translation of any text by using one picture per character. It is even easier when we call each picture by the name of the character it represents.

The following quick and dirty JavaScript will do the trick.

function SimpleBraille(message) 
{ 
    var myHTML = ""; 
    for (var i=0; i<message.length; i++) 
    {
        if (message.charAt(i)==" ")
            myHTML += "<IMG  SRC=\"braillepix/sp.gif\">";
        else 
            myHTML += "<IMG SRC=\"braillepix/" + message.charAt(i).
                toLowerCase() + ".gif\">";
    } 
    return myHTML;
}

Improving the JavaScript

Of course, this was an over-simplification to demonstrate the way it works. Let's now make a few improvements the code.

There are a few extra rules we should add to our script

  • Uppercases must be prefixed by a special Braille character
  • Numbers are displayed using "a" for "1", "b" for "2"... prefixed with a special character
  • Punctuation signs use more special characters

While we are at it, we can also optimize the code

  • Let's add the "ALT" attribute in the generated "IMG" tags, to see the translation of each character on mouse-over.
  • Let's use and array, push and join instead of string concatenation, so it will be much faster.

The improved JavaScript function is now much longer but will produce more accurate Braille.

function Braille(message) 
{ 
    var brailleArr = []; 
    var myChar, prevCharNum, inQuote

        function BrailleChar(bPix,bAlt) 
    {
        return ["<IMG HEIGHT=27 WIDTH=19 vspace=5 hspace=1 SRC=\"braillepix/", 
            bPix, ".gif\" ALT=\"", bAlt, "\">"].join("");
    }

    for (var i=0; i<message.length; i++) 
    {
        myChar = message.charAt(i);
        if ((myChar>="a") && (myChar<="z")) 
        {       // a to z

            brailleArr.push(BrailleChar(myChar, myChar));
            prevCharNum = false;            
        } else if((myChar>="A") && (myChar<="Z")) 
        {       // A to Z

            brailleArr.push(BrailleChar("cap", "Caps"));
            brailleArr.push(BrailleChar(myChar, myChar));            
            prevCharNum = false;         
        } else if((myChar>"0") && (myChar<="9")) 
        {
            if (!prevCharNum)
                brailleArr.push(BrailleChar("num", "Number"));
            brailleArr.push(BrailleChar(String.fromCharCode(myChar.
                charCodeAt(0) + 48), myChar)); 
            prevCharNum = true;            
        } else 
        {
            switch (myChar) 
            {
            case " ": 
                brailleArr.push(BrailleChar("sp", "Space"));
                prevCharNum = false;
                break;
            case "0":
                if (!prevCharNum)
                    brailleArr.push(BrailleChar("num", "Number")); 
                brailleArr.push(BrailleChar("j", "0"));      
                prevCharNum = true;
                break;
            case "\n":
                brailleArr.push("<br><br>");
                nbCharsInLine = -1;
                prevCharNum = false;
                break;
            case ".":
                if (prevCharNum)
                    brailleArr.push(BrailleChar("dec", ".")); 
                else
                    brailleArr.push(BrailleChar("period", "."));  
                break;
            case "$":
                brailleArr.push(BrailleChar("period", "$"));
                prevCharNum = false;
                break;
            case "%":
                brailleArr.push(BrailleChar("col", "%"));
                brailleArr.push(BrailleChar("p", ""));
                prevCharNum = false;
                break;
                case "'":
                    brailleArr.push(BrailleChar("qs", "'")); 
                    prevCharNum = false;
                    break;
            case ",":
                brailleArr.push(BrailleChar("comma", ","));
                prevCharNum = false;
                break;
            case "?":
                brailleArr.push(BrailleChar("qu", "?")); 
                prevCharNum = false;
                break;
            case "(":
            case ")":
                brailleArr.push(BrailleChar("par", "parenthesis")); 
                prevCharNum = false;
                break;
            case "*":
                brailleArr.push(BrailleChar("ast", "*"));
                brailleArr.push(BrailleChar("ast", "*")); 
                prevCharNum = false;
                break;
                case "//":
                    brailleArr.push(BrailleChar("sla", "//")); 
                    prevCharNum = false;
                    break;
            case "!":
                brailleArr.push(BrailleChar("ex", "!")); 
                prevCharNum = false;
                break;
                case "'": 
                    if (inQuote)
                        brailleArr.push(BrailleChar("qc", "Close Quote")); 
                    else
                        brailleArr.push(BrailleChar("qo", "Open Quote"));  
                    inQuote = !inQuote;
                    prevCharNum = false;
                    break;
            case ":":
                brailleArr.push(BrailleChar("col", ":"));
                prevCharNum = false;
                break;
            case ";":
                brailleArr.push(BrailleChar("sc", ";")); 
                prevCharNum = false;
                break;
            case "[":
                brailleArr.push(BrailleChar("cap", "["));
                brailleArr.push(BrailleChar("par", ""));
                break;
            case "]":
                brailleArr.push(BrailleChar("par", "]"));
                brailleArr.push(BrailleChar("qs", "")); 
                break;
            }
        }
    }
    return brailleArr.join("");
}

I'm not a Braille expert, but I know there are other rules that could be added. There is still room for improvement.

Using the code

The JavaScript is rather straight forward and simple enough to work on any browser. What took a while was making the pictures of all the different Braille characters, but don't worry these are included in the download, and they even come in three different flavors. Simply change the directory name in the JavaScript to use the set of pictures you prefer.

Screenshot - braille-black.gifBlack

Screenshot - braille-3d.gif3D

Screenshot - braille-big.gifBig

Using the Braille function, we can display the classic "Hello World" as follow:

document.write(Braille("Hello World"));

Otherwise, you may use it to show your users how their name looks like in Braille.

Screenshot - seeyourname.gif

Try this code live at EvolUtility

Eventually, if you really want to learn Braille, I recommend starting with the book Braille for the Sighted from Schneider, and Kathy Kifer. Enjoy.

License

This article, along with any associated source code and files, is licensed under The MIT License

About the Author

Olivier Giulieri


I'm a web developer who loves UI and databases... and what I really enjoy is to describe UI in metadata, store it (outside of the code) in a database or in XML, then dynamically generate screens at run-time.

I'm currently working on Evolutility an open source metadata driven web UI for CRUD.

In Biology, Evolutility means "The faculty possessed by all substances capable of self-nourishment of manifesting the nutritive acts by changes of form, of volume, or of structure".

In open source, Evolutility will now be a tool which feeds on metadata to change web form, structure, and volume... an application which can modify itself.

My articles on Evolutility:

Occupation: Software Developer (Senior)
Location: United States United States

Other popular Client side scripting articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 7 of 7 (Total in Forum: 7) (Refresh)FirstPrevNext
GeneralReversed BraillememberGuy Winters15:12 26 Jan '08  
GeneralRe: Reversed BraillememberOlivier Giulieri13:14 27 Jan '08  
QuestionA question on the application's usabilitymembervolkan.ozcelik22:59 11 Apr '07  
AnswerRe: A question on the application's usabilitymemberOlivier Giulieri8:23 12 Apr '07  
AnswerRe: A question on the application's usabilitymemberOlivier Giulieri20:31 12 Apr '07  
GeneralThank you!memberaraud19:43 9 Apr '07  
GeneralRe: Thank you!memberOlivier Giulieri21:42 9 Apr '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 2 Apr 2007
Editor: Sean Ewington
Copyright 2007 by Olivier Giulieri
Everything else Copyright © CodeProject, 1999-2008
Web12 | Advertise on the Code Project