Uhfgood

Bitmap text with embedded base 64 image

Jan 24th, 2023
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.         // character set image in base64 -- 144x256 image of IBM VGA Code Page 37 with a matrix of 16x16 characters each with a width of 9px wide by 16px high
  2.  
  3.         // note this is not the actual full base 64 string, as it would be too large to paste in here however,
  4.         // if you want to see the full base string, you can just ask me, and I'll be happy to send it
  5.         let baseString = "data:image/png;base64,iVBORw0KGgoAAAANggg=="; // note: is truncated ^^
  6.         // create a texture from our base64 encoded image
  7.         let newTex = PIXI.Texture.from( baseString );
  8.         // nearest so it doesn't become blurry or filtered
  9.         newTex.baseTexture.scaleMode = PIXI.SCALE_MODES.NEAREST;
  10.        
  11.         // create the font descriptor in xml format
  12.         let objStr =
  13.             `<?xml version="1.0"?>
  14.             <font>
  15.                 <info face="Px437" size="16" bold="0" italic="0" charset="ASCII" unicode="0" stretchH="100" smooth="0" aa="0" padding="0,0,0,0" spacing="0,0"/>
  16.                 <common lineHeight="16" base="12" scaleW="144" scaleH="256" pages="1" packed="0"/>
  17.                 <pages>
  18.                     <page id="0" file=""/>
  19.                 </pages>
  20.                 <chars count="256">`;
  21.  
  22.         // generating each character descriptor, which is easy programatically since the chars are uniform 9x16 px
  23.         for( var i = 0; i < 256; i++ )
  24.         {
  25.             let x = ( i % 16) * 9;
  26.             let y = Math.floor( i / 16 ) * 16;
  27.             objStr += "\n\t\t\t\t\t";
  28.             objStr += ( `<char id="` + i + `" x="` + x + `" y="` + y + `" width="9" height="16" xoffset="0" yoffset="0" xadvance="9" page="0" chnl="15"/>` );
  29.         }
  30.  
  31.         objStr +=
  32.             `  
  33.                 </chars>
  34.             </font>`;
  35.  
  36.         // so apparently most if not all browsers have a parse function to do either html or xml
  37.         let xp = new DOMParser();
  38.  
  39.         // so now we parse the xml into an XMLDocument type object
  40.         let xd = xp.parseFromString( objStr, "text/xml" );
  41.  
  42.         // this is for pixi 5 so if I end upgrading to later versions, I will probably have to change this
  43.         PIXI.BitmapText.registerFont( xd, newTex );
  44.  
  45.         function GenerateCharacterLookup() : Map<string, string>
  46.         {
  47.             // so these are the "visible" code page 437 characters -- with the exception of 11 and 13 which can't be displayed because they're
  48.             // line feed and carriage return characters -- so those two characters I've copied from two other characters.  One was a musical note
  49.             // and the other was the one that is a square with a circle in it.
  50.             let codePage437Chars = " ☺☻♥♦♣♠•◘○■♂♀♫♫☼►◄↕‼¶§▬↨↑↓→←∟↔▲▼ !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~⌂ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜ¢£¥₧ƒáíóúñѪº¿⌐¬½¼¡«»░▒▓│┤╡╢╖╕╣║╗╝╜╛┐└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀αßΓπΣσµτΦΘΩδ∞φε∩≡±≥≤⌠⌡÷≈°∙·√ⁿ²■"
  51.  
  52.             // the key will be the unicode version or the 'visible' version of the keys you want to convert
  53.             // the value will be these characters after their converted into their ascii equivalents.
  54.             let lookupTable = new Map<string, string>();
  55.  
  56.             // iterate through all characters
  57.             for (let i = 0; i < codePage437Chars.length; i++)
  58.             {
  59.                 // the characters as you would see them for the key
  60.                 let char = codePage437Chars[i];
  61.                
  62.                 // ascii equivalent -- technically is also unicode, but cover chars 0-255
  63.                 let asciiEqu = String.fromCharCode(i);
  64.  
  65.                 // so let's build the table
  66.                 lookupTable.set(char, asciiEqu);
  67.             }
  68.  
  69.             return lookupTable;
  70.         }
  71.  
  72.         let characterLookup = GenerateCharacterLookup();
  73.  
  74.         // this will take the string you want to display
  75.         function ConvertString( myString : string, lookup : Map<string, string> ) : string
  76.         {
  77.             let returnString = "";
  78.  
  79.             // go through your string
  80.             for( let i = 0; i < myString.length; i++ )
  81.             {
  82.                 // and get the proper ascii position, to get the character
  83.                 // representing what's in the table.
  84.                 returnString += lookup.get( myString.charAt( i ) );
  85.             }
  86.  
  87.             return returnString;
  88.         }
  89.  
  90.  
  91.         // so now we have our string
  92.         let testStr = ConvertString( "«░▒▓»▲▲▲ UP / DOWN ▼▼▼«▓▒░»", characterLookup );
  93.  
  94.         // we also have our font that we registered before hand
  95.         let bitmapText = new PIXI.BitmapText( testStr, { font : { name : "Px437", size : 16 }, align : "left" } );
  96.         bitmapText.x = 512+58;
  97.         bitmapText.y = 200;
  98.         this._uiLayer.addChild( bitmapText );
  99.  
Add Comment
Please, Sign In to add comment