Difference between revisions of "HexText/HexText 2XKanji"

From Second Life Wiki
Jump to navigation Jump to search
Line 5: Line 5:
////////// DESCRIPTION /////////////////////////////////////
////////// DESCRIPTION /////////////////////////////////////
//                                                        //
//                                                        //
//  HexText 1.0 Unicode Display System                   //
//  HexText 1.0 Unicode Display System by Hexadeci Mole  //
//  Two faces for Kanji, one for all other characters    //
//  Two faces for Kanji, one for all other characters    //
//  Thanks to Ochi Wolfe for the FURWARE text toolset    //
//  Thanks to Ochi Wolfe for the FURWARE text toolset    //

Revision as of 18:30, 5 October 2020

HexText Unicode Display (2XKanji). Two prim faces for Kanji/Katakana, 1 prim face for all other characters.

////////// DESCRIPTION /////////////////////////////////////
//                                                        //
//  HexText 1.0 Unicode Display System by Hexadeci Mole   //
//  Two faces for Kanji, one for all other characters     //
//  Thanks to Ochi Wolfe for the FURWARE text toolset     //
//                                                        //
////////////////////////////////////////////////////////////

////////// DOCUMENTATION ///////////////////////////////////
//                                                        //
//  Documentation is available at:                        //
//  http://wiki.secondlife.com/wiki/HexText               //
//                                                        //
////////////////////////////////////////////////////////////

////////// LICENSE /////////////////////////////////////////

/*
MIT License
Copyright (c) 2018-2019 Hexadeci Mole, HexText
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

////////// CONSTANTS ///////////////////////////////////////

// Numerically accurate for Furware faces, but there can be a bit of character cell bleed
vector  gOffset = <0.53125, 0.0123, 0.0>;
vector  gCellSize = <0.0625, 0.0625, 0.0>;
vector  gOffsetLeft = <0.015625, 0.0123, 0.0>;
vector  gOffsetRight = <-0.015625, 0.0123, 0.0>;
vector  gCellSizeHalf = <0.03125, 0.0625, 0.0>;

// Numerically accurate for normal UV faces, but there can be a bit of character cell bleed
//vector  gOffset = <0.46875, 0.46875, 0.0>;
//vector  gCellSize = <0.0625, 0.0625, 0.0>;
//vector  gOffsetLeft = <0.484375, 0.46875, 0.0>;
//vector  gOffsetRight = <0.453125, 0.46875, 0.0>;
//vector  gCellSizeHalf = <0.03125, 0.0625, 0.0>;

////////// DEFAULTS ////////////////////////////////////////

rotation gColor = <1.0, 1.0, 1.0, 1.0>;	// <R, G, B, A>
integer gAlign = 0;			// Alignment: 0 = Left, 1 = Center, 2 = Right
integer gWrap = 2;			// Wrapping: 0 = None, 1 = Char, 2 = Word
integer gNotify = FALSE;	// Notify when drawing is completee
integer gTrim = TRUE;		// Trim whitespace from lines
integer gForce = FALSE;    	// Ignore optimizations for drawing
float   gWrapLimit = 0.75; 	// Percentage of display width before which word wrap is cancelled.

////////// END USER CONFIG /////////////////////////////////

integer gPrimCount;
integer gCols;
integer gColPrims;
integer gRows;
vector	gSpaceOffset;
vector	gBlankColor = <1.0, 1.0, 1.0>;

list    gTextLinks;
list    gRowLengths;

list    gCodePages;
list    gCodePageUUIDs = [
            0x00, "27e9e604-b315-454f-770e-cf5dc6333879",
            0x01, "fdcf478b-302d-70f8-fb72-c2cbf403d242",
            0x02, "10b6e068-571f-c912-1177-c25533b5350b",
            0x03, "ed4b350a-d0cb-68fa-e01c-7a4662837ad6",
            0x04, "090834da-5503-aeb9-8f42-f979c76a0f35",
            0x05, "d2378c82-ce3a-19c2-a25c-ea84b5d87b39",
            0x06, "93917f55-2427-365b-6d81-46871d3eb8d7",
            0x0E, "deadeda9-4676-9f75-b11d-0c1c4cc320af",
            0x10, "03088af6-b6e2-7a93-3ac4-b275b650a12b",
            0x14, "92c4d466-02c3-6063-440d-686a17287ef3",
            0x15, "96424a86-271d-2d64-4807-d6710e27afd8",
            0x16, "b7f7a0e7-e528-a504-2c52-171ea216982b",
            0x1D, "756ad03c-2e87-1bd2-8265-edf77f9493e6",
            0x1E, "6ba77713-1017-72d5-96c0-9b9305659e1e",
            0x1F, "047f7d75-cfcd-5515-227f-668554d7427d",
            0x20, "f40f5d7d-58e5-68e3-5c21-ba2a0f18e0a9",
            0x21, "6fce7405-d606-bb55-8ddd-f32f444b78da",
            0x22, "bb7bea3c-9044-e505-41d6-afafd6a9f5de",
            0x23, "5ca29772-2722-011f-8e04-3bfe1e713864",
            0x24, "0dede3b3-e632-8059-1a33-125f3e100e52",
            0x25, "87a27d99-2e97-2613-c47b-63806981b6e5",
            0x26, "8c382389-5701-4d3a-62d2-6e8720dc665f",
            0x27, "c76498fc-03a3-f598-fa7d-24b4876f0c26",
            0x2C, "7a702e38-9304-cbde-e5ab-a60a020f1d1d",
            0x30, "bbed1e3f-f89b-49c4-999c-80cc352189f6",
            0x31, "504c15c1-e9b5-8691-8b9a-574b0eb16633",
            0x32, "3f47af69-cb2e-8e85-d2d9-09c869822419",
            0x33, "eb7c701e-ac4a-9c2b-19a9-afc7f08c4675",
            0x34, "2cf174bd-0deb-2970-d63d-e359f7512521",
            0x35, "1f9bb5a7-2f4a-15e3-8d6d-504cd0d250de",
            0x36, "99d49a10-42bb-45a9-03ed-c81dea3f2d12",
            0x37, "441e23c4-0608-59fe-7e56-99e53f1c71ba",
            0x38, "9582249b-5370-dd77-0925-c97e72f6d8e9",
            0x39, "3c79958e-bdf2-73cd-2e6c-f2acc297bfcd",
            0x3A, "2286bbb7-f805-690f-0b0b-0593c12e2f2a",
            0x3B, "a2e5a0d0-f5aa-913f-1c00-f641f2ca5091",
            0x3C, "955e9609-afde-ebb7-a5e1-ddae46455090",
            0x3D, "431e9f87-41a2-a7f3-22ad-c17b4ec5a237",
            0x3E, "b5db2e3b-2271-a0bd-00d6-8b40812097ac",
            0x3F, "76f2d1fe-826d-f30a-c1af-1b09387c2936",
            0x40, "f90e3a5e-9404-0729-0ddc-46f52fc8e919",
            0x41, "70a5bc82-7abb-763b-fd46-12c9e4c60a84",
            0x42, "1b4af3ed-1ca2-54da-2f13-2c378476c50b",
            0x43, "33c44a35-5bdd-3cf0-9e20-bbebc4390ae7",
            0x44, "78468268-b9dc-57f4-554a-097341f9e37f",
            0x45, "be4f77d7-e86d-9c19-50f4-ff70fa0bbcdd",
            0x46, "cecc557a-e740-3468-787a-a8efc50afb82",
            0x47, "04d78b83-693d-8c16-4a1f-634c5dd6615a",
            0x48, "7e01d0db-b456-3cb8-b97c-f688dc27f705",
            0x49, "55d04868-f003-4de0-5237-a04862cc4697",
            0x4A, "afa697ab-2885-8d7c-f59b-b0f48b94941f",
            0x4B, "07f7427f-fc20-9d10-7ffd-556fb3c480a9",
            0x4C, "ff6b4d03-ca92-e7d4-46e9-9121352dec6e",
            0x4D, "c41ac88b-4e71-d997-cd33-fd1d81ca992c",
            0x4E, "c2309d45-43b7-15f4-e482-886dbf9b8419",
            0x4F, "3190730d-de06-ea8a-3a5d-757b5ef9c04d",
            0x50, "dcdf69a0-83c0-68d3-7883-9d6b142ab522",
            0x51, "8214ba5e-af73-0ed3-84f6-1251d668ce09",
            0x52, "0aa80f45-305c-a8b7-fdc5-c49cdb837d43",
            0x53, "261cb0e8-a2d7-b90d-734f-409064920bec",
            0x54, "0ad4e700-c8db-dc9f-6dea-45d126ce1781",
            0x55, "81f351a3-4a90-e052-22d7-daa869ee3656",
            0x56, "7df297e5-79cd-7036-9443-f0f8a9fa7e8f",
            0x57, "e06b0624-4363-2202-96b9-47b1321abafe",
            0x58, "9dfa4ed2-5146-0c20-4c1b-d67611591a2d",
            0x59, "7a0d489d-bf9c-4e76-bc30-65f9606a77a9",
            0x5A, "ab196901-2d9c-23d5-a10a-e28fd7b784a9",
            0x5B, "c569ab67-1c18-307a-0d57-bafa4e0320e7",
            0x5C, "cc92b193-1ed3-72ed-428f-de27c971e53e",
            0x5D, "3d9233a1-acbf-e28b-bfd9-db9d080daaaf",
            0x5E, "7da71ac2-af51-48b8-5207-70991b2b172b",
            0x5F, "cf34309e-b3e7-9777-6b04-2a64893ccc83",
            0x60, "6a449a86-c107-d264-ff67-8241347d865d",
            0x61, "e4f9c21d-8508-e64b-d390-3c85f06dea17",
            0x62, "c3a3ce13-6dec-8e81-d2a4-3125559bb782",
            0x63, "52bff60e-65c7-a540-ff35-95c5a8220f81",
            0x64, "9aeca54a-3ed9-6692-098b-2ec3da979cb3",
            0x65, "20c82000-a1d8-9cc3-bf40-e4b4e2472018",
            0x66, "58ae46d4-2e5b-cacb-101a-6cef0987d75a",
            0x67, "cf8f267d-6b6f-fd88-8588-48e886dc3010",
            0x68, "e603db4d-1e85-516a-b612-4b1b748fc3e2",
            0x69, "4122d3ed-68b9-2eba-dd7d-b8a0f59126a2",
            0x6A, "6a4425cf-46c7-1dd3-1e15-bc58b6e63522",
            0x6B, "3402c9d6-b776-2f3f-7d8d-855cd9a7f7e7",
            0x6C, "09710c77-9db2-65ff-db0b-400f42b55671",
            0x6D, "b0fca79a-5060-324f-f4f5-71cffac18e1f",
            0x6E, "cd8d52f4-32d1-4ea3-6908-8ed9204d4f8f",
            0x6F, "90e38c28-d6a9-fca4-0e39-b24adf825ba8",
            0x70, "b25aadb4-f64a-6420-b479-277edea2a169",
            0x71, "ced5dcf5-58a8-d7b3-0da8-cb10f9b708f8",
            0x72, "515461e6-1aca-5056-d289-705a1c070ef4",
            0x73, "226100cc-10ce-8cb7-f7a8-d13873493374",
            0x74, "37d54277-71b2-dd6f-ebff-666aa4c6efd0",
            0x75, "38b2a124-4ef3-9dc0-e4c4-b4d1ee25811a",
            0x76, "d8c218ff-97b0-f320-7f2e-ba74fcd3e19c",
            0x77, "8d68593e-90fb-c442-b320-c6e99cd831b2",
            0x78, "a1b3619d-eb8f-03c2-e219-375ab2e978c3",
            0x79, "b25f989c-ea35-3398-7809-d0e47415e1ec",
            0x7A, "b3d35e10-70f0-4004-3d4f-8987e77c3ce6",
            0x7B, "6ff601ba-64fb-f61b-c11a-95c4e26cc3b5",
            0x7C, "5c5508de-ca4a-1a85-3662-7767af1d45f1",
            0x7D, "f334c84a-8a89-4dca-1a0a-3f57869b383a",
            0x7E, "6d4992fc-1891-7bb8-e8c8-080b0c7f0f53",
            0x7F, "1c087c0c-2f44-70c6-f386-9fff328645af",
            0x80, "ae6217d8-a650-393d-4c12-244924e50091",
            0x81, "5bac0887-f686-1486-123d-c9cc203a97d7",
            0x82, "4c00bbf3-25d7-c781-ad69-f2c8b151844d",
            0x83, "02146f13-b5a4-34d4-eae3-b87e8a622446",
            0x84, "149a8ebe-fa15-b985-b6ca-4b4f8c8dcbc6",
            0x85, "899f9215-92ca-39bc-b157-46b4e56ffee2",
            0x86, "07c72032-8ba0-58b5-7dbb-1e26d4a9c29d",
            0x87, "021ca04d-5893-f615-9357-5ec1fd1e0985",
            0x88, "f13f10ab-8526-a061-b90e-38b0f13876a9",
            0x89, "9d8895f4-1080-69f7-0f78-cc7c42068402",
            0x8A, "35363fbc-f952-3833-f2b8-ac1685decb41",
            0x8B, "ece7161f-2360-e294-55f8-18593cfe268e",
            0x8C, "b199f897-ce70-3715-05e0-a58c234e21e7",
            0x8D, "066676e6-5640-399e-0191-b061e56a9277",
            0x8E, "b5e67788-bd9b-9603-9f5f-8c8fc90dbcdf",
            0x8F, "180bb534-f8fb-b8cd-fcb9-938eb41876f6",
            0x90, "42d6a5c7-3537-0e6a-ceb1-c7ec1408843e",
            0x91, "28d50a1f-aae9-e9ae-df70-dad6d0dd494b",
            0x92, "4638765a-bf63-95fb-dfef-df43e3fd6b0e",
            0x93, "c183e0ac-c3bf-cbcc-79d8-2877e5aeaa87",
            0x94, "db5df7a0-b48e-28ac-e24a-0222a3cda022",
            0x95, "3f5e9f00-86bc-b5a4-8930-61a8c2f94dbc",
            0x96, "d92d47bf-0582-fa1c-5e62-3ef591f2a12b",
            0x97, "a2603a86-209d-d36c-2df8-03ad69418439",
            0x98, "d1b3c91d-3d5a-53f8-9fdc-149fb77cfdfc",
            0x99, "c4a69385-91f1-375c-b9d6-86eb1b8ed537",
            0x9A, "4c9dd325-deb5-b5b3-7b94-c6dd0aec5a99",
            0x9B, "8ba75a75-1421-bc42-6a41-3cef5ff131ad",
            0x9C, "33aecc7d-ab14-c2d8-1428-1379a73a69a2",
            0x9D, "dbb8d20e-e182-67ec-955d-33de256e6241",
            0x9E, "9dd876bf-f802-4cd6-5395-ba00d5e1b3b2",
            0x9F, "a60a2bea-4e28-78a1-d9da-887b60a219ef",
            0xA6, "24f26d30-c396-b3fb-42b6-f3ba01b7ed87",
            0xA7, "39102d6d-227b-86f1-e44b-324a7bf58bf4",
            0xF8, "21ad1fb8-214f-c9db-74d5-317e692d7bb7",
            0xFE, "33d6e3ce-e1bb-816a-6133-1af0077a67b5",
            0xFF, "5709c611-6a62-244f-5963-dda110a59f04"];

string  gBlankUUID = "a2d3fdcf-febe-1481-09c7-a662f352f15e"; // Used for unrendered Unicode blocks.
string  gASCII_UUID; // Used to speed up rendering for ASCII, set to 0x00 code page above.

list    gColors = [
			"rand",			<0.0, 0.0, 0.0, 1.0>,
            "white",        <1.0, 1.0, 1.0, 1.0>,
            "black",        <0.0, 0.0, 0.0, 1.0>,
            "darkred",      <0.5, 0.0, 0.0, 1.0>,
            "darkgreen",    <0.0, 0.5, 0.0, 1.0>,
            "darkblue",     <0.0, 0.0, 0.5, 1.0>,
            "darkcyan",     <0.0, 0.5, 0.5, 1.0>,
            "darkmagenta",  <0.5, 0.0, 0.5, 1.0>,
            "darkyellow",   <0.5, 0.5, 0.0, 1.0>,
            "gray",         <0.5, 0.5, 0.5, 1.0>,
            "red",          <1.0, 0.0, 0.0, 1.0>,
            "green",        <0.0, 1.0, 0.0, 1.0>,
            "blue",         <0.0, 0.0, 1.0, 1.0>,
            "cyan",         <0.0, 1.0, 1.0, 1.0>,
            "magenta",      <1.0, 0.0, 1.0, 1.0>,
            "yellow",       <1.0, 1.0, 0.0, 1.0>,
            "silver",       <0.75, 0.75, 0.75, 1.0>];

Flush()
{
    integer i = 0; 
    gRowLengths = [];
    
    while (i++ < gRows) gRowLengths += 0xFFFF;
}

Render(list chars, integer col, integer row, integer direct)
{
    if (col < 0 || col >= gCols || row < 0 || row >= gRows) return;
    
    integer len = llGetListLength(chars);
    integer cursor;
    integer unicode;
    integer i;
    integer row_index = row * gColPrims;
    integer clear = (gForce || len < llList2Integer(gRowLengths, row)) && !direct; // Never clear if using direct access
    string  input;
    string  char;
    list    params;
    
    if      (direct)        cursor =  col;
    else if (gAlign == 0 || len == 0) cursor =  0;
    else if (gAlign == 1)   cursor = (gCols - len) / 2;
    else                    cursor =  gCols - len;
    
    if (cursor + len > gCols) len = gCols - cursor;
    
    // Using direct invalidates optimization for this row.
    if (direct) gRowLengths = llListReplaceList(gRowLengths, [0xFFFF], row, row);
    else        gRowLengths = llListReplaceList(gRowLengths, [len], row, row);
    
    // Clear out everything before first char
    // Clear routines don't really need to set PRIM_TEXTURE, but doing so prevents problems with
    // scripts using llSetAlpha to hide/show an entire linkset
    if (cursor > 0 && clear)
    {
        while (i < cursor)
        {
            if (i & 7)
            {
                // Clear out individual faces
                params += [ PRIM_TEXTURE, i & 7, gASCII_UUID, gCellSize, gSpaceOffset, 0.0, PRIM_COLOR, i & 7, gBlankColor, 0.0 ];
                ++i;
            }
            else
            {
            	// New prim, issue PRIM_LINK_TARGET
                params += [ PRIM_LINK_TARGET, llList2Integer(gTextLinks, row_index + (i >> 3)) ];
                if (i + 7 < cursor)
                {
                    // Deal with whole prims when possible
                    params += [ PRIM_TEXTURE, ALL_SIDES, gASCII_UUID, gCellSize, gSpaceOffset, 0.0, PRIM_COLOR, ALL_SIDES, gBlankColor, 0.0 ];
                    i += 8;
                }
                else
                {
                    // Clear out individual faces
                    params += [ PRIM_TEXTURE, i & 7, gASCII_UUID, gCellSize, gSpaceOffset, 0.0, PRIM_COLOR, i & 7, gBlankColor, 0.0 ];
                    ++i;
                }
            }
        }
        
        llSetLinkPrimitiveParamsFast(0, params);
        params = [];
    }
    
    // Draw the line
    for (i = 0; i < len; ++i)
    {
        unicode = llList2Integer(chars, i);
        if (!(unicode & 0x30000000))
        {
            params += [ PRIM_TEXTURE, cursor & 7, llList2String(gCodePages, unicode >> 8 & 0xFF),
                        gCellSize, <(unicode & 0xF) * 0.0625 - gOffset.x, gOffset.y - (unicode >> 4 & 0xF) * 0.0625, 0.0>, 0.0,
                        PRIM_COLOR, cursor & 7, <gColor.x, gColor.y, gColor.z>, gColor.s ];
        }
        else if (unicode & 0x10000000)
        {
            params += [ PRIM_TEXTURE, cursor & 7, llList2String(gCodePages, unicode >> 8 & 0xFF),
                        gCellSizeHalf, <(unicode & 0xF) * 0.0625 - gOffsetLeft.x, gOffsetLeft.y - (unicode >> 4 & 0xF) * 0.0625, 0.0>, 0.0,
                        PRIM_COLOR, cursor & 7, <gColor.x, gColor.y, gColor.z>, gColor.s ];
        }
        else
        {
            params += [ PRIM_TEXTURE, cursor & 7, llList2String(gCodePages, unicode >> 8 & 0xFF),
                        gCellSizeHalf, <(unicode & 0xF) * 0.0625 - gOffsetRight.x, gOffsetRight.y - (unicode >> 4 & 0xF) * 0.0625, 0.0>, 0.0,
                        PRIM_COLOR, cursor & 7, <gColor.x, gColor.y, gColor.z>, gColor.s ];
        }
        
        if (cursor & 7 == 7)
        {
            llSetLinkPrimitiveParamsFast(llList2Integer(gTextLinks, row_index + (cursor >> 3)), params);
            params = [];
        }
        
        ++cursor;
    }
    
    // If there are any faces left to draw, set them.
    if (llGetListLength(params)) llSetLinkPrimitiveParamsFast(llList2Integer(gTextLinks, row_index + (cursor >> 3)), params);
    params = [];
    
    // Clear out everything after last char
    if (cursor < gCols && clear)
    {
        i = cursor;
        if (i & 7) params = [ PRIM_LINK_TARGET, llList2Integer(gTextLinks, row_index + (i >> 3)) ];
        
        while (i < gCols)
        {
            if (i & 7)
            {
            	// Clear out individual faces
                params += [ PRIM_TEXTURE, i & 7, gASCII_UUID, gCellSize, gSpaceOffset, 0.0, PRIM_COLOR, i & 7, gBlankColor, 0.0 ];
                ++i;
            }
            else
            {
            	// New prim, issue PRIM_LINK_TARGET
                params += [ PRIM_LINK_TARGET, llList2Integer(gTextLinks, row_index + (i >> 3)) ];
                if (i + 7 < gCols)
                {
                    // Deal with whole prims when possible
                    params += [ PRIM_TEXTURE, ALL_SIDES, gASCII_UUID, gCellSize, gSpaceOffset, 0.0, PRIM_COLOR, ALL_SIDES, gBlankColor, 0.0 ];
                    i += 8;
                }
                else
                {
                    // Clear out individual faces
                    params += [ PRIM_TEXTURE, i & 7, gASCII_UUID, gCellSize, gSpaceOffset, 0.0, PRIM_COLOR, i & 7, gBlankColor, 0.0 ];
                    ++i;
                }
            }
        }
        
        llSetLinkPrimitiveParamsFast(0, params);
    }
}

list UTFtoInt(string line)
{
    integer i;
    integer unicode;
    string  input;
    integer len = llStringLength(line);
    list    chars;
    
    for (i = 0; i < len; ++i)
    {
        // Unicode Decoder: http://wiki.secondlife.com/wiki/UTF-8 
        unicode = llBase64ToInteger(llStringToBase64(input = llGetSubString(line, i, i)));
        if(unicode & 0x80000000) //multibyte, continuing to use base64 is impractical because it requires smart shifting.
        {
            unicode = (   (  0x0000003f &  unicode       ) |
                          (( 0x00003f00 &  unicode) >> 2 ) |
                          (( 0x003f0000 &  unicode) >> 4 ) |
                          (( 0x3f000000 & (unicode = (integer)("0x"+llGetSubString(input,-8,-1)))) >> 6 ) |
                          (( 0x0000003f &  unicode) << 24) |
                          (( 0x00000100 & (unicode = (integer)("0x"+llDeleteSubString(input = (string)llParseString2List(llEscapeURL(input),["%"],[]),-8,-1)))) << 22)
                      ) & (  0x7FFFFFFF >> (5 * ((integer)(llLog(~unicode) / 0.69314718055994530941723212145818) - 25)));
            
            if (unicode > 0x2FFF && unicode < 0xA000) chars += [unicode | 0x10000000, unicode | 0x20000000];
            else chars += unicode;
        }
        else
        {
            chars += unicode >> 24;
        }
    }
    
    return chars;
}

list ListTrim(list chars, integer type)
{
	if (type & 0x1)
	{
		while (llGetListLength(chars) && llList2Integer(chars,  0) <= 32) llDeleteSubList(chars,  0,  0);
	}
	
	if (type & 0x2)
	{
		while (llGetListLength(chars) && llList2Integer(chars, -1) <= 32) llDeleteSubList(chars, -1, -1);
	}
	
	return chars;
}
 
Draw(string text)
{
    integer row;
    list    lines;
    list    chars;
    integer char;
    integer line_num;
    integer pos;
    list    out;
    
    lines = llParseStringKeepNulls(text, [ "\n", "<br>" ], []);
    line_num = 0;
    row = 0;
    do
    {
        // For future right to left language support, we could test
        // last char of a line to see if it is in a RTL unicode block,
        // and then use a different do block to work through them right to left.
        // It would be especially important when splitting lines to display
        // them in the correct order.
        
        if (gTrim) chars = ListTrim(UTFtoInt(llList2String(lines, line_num)), STRING_TRIM);
        else chars = UTFtoInt(llList2String(lines, line_num));
        
        do
        {
            if (llGetListLength(chars) <= gCols)
            {
                Render(chars, 0, row, FALSE);
                chars = [];
            }
            else if (llList2Integer(chars, gCols) == 20 || gWrap < 2)
            {
                if (gTrim)  Render(ListTrim(llList2List(chars, 0, gCols - 1), STRING_TRIM), 0, row, FALSE);
                else        Render(llList2List(chars, 0, gCols - 1), 0, row, FALSE);
                
                if (gWrap)
                {
                    // If we got here with wrap = 2, then there was a space, and we want to skip it.
                    chars = llList2List(chars, gCols + (gWrap == 2), -1);
                }
                else
                {
                    chars = [];
                }
            }
            else
            {
                pos = gCols - 1;
                out = [];
                
                do
                {
                    char = llList2Integer(chars, pos) & 0xFFFF;
                    
                    if (char == 32)
                    {
                        out   = llList2List(chars, 0, pos - 1);
                        chars = llList2List(chars, pos + 1, -1);
                    }
                    else if (char == 45) // Japanese: "、" and "。", Hebrew uses "־‬", Armenian uses "֊"
                    {
                        out   = llList2List(chars, 0, pos);
                        chars = llList2List(chars, pos + 1, -1);
                    }
                    else if (pos < gCols * gWrapLimit)
                    {
                        // Test to see if char is second half of split char.
                        if (llList2Integer(chars, gCols - 1) & 0x10000000)
                        {
                            out   = llList2List(chars, 0, gCols - 2);
                            chars = llList2List(chars, gCols - 1, -1);
                        }
                        else
                        {
                            out   = llList2List(chars, 0, gCols - 1);
                            chars = llList2List(chars, gCols, -1);
                        }
                    }
                }
                while (!llGetListLength(out) && --pos >= 0);
                
                if (gTrim)  Render(ListTrim(out, STRING_TRIM), 0, row, FALSE);
                else        Render(out, 0, row, FALSE);
            }
            
            ++row;
        }
        while (llGetListLength(chars) && row < gRows);
    }
    while (++line_num < llGetListLength(lines));
    
    // Blank out any remaining display lines
    while (row < gRows)
    {
        Render([], 0, row, FALSE);
        ++row;
    }
    
    if (gNotify) llMessageLinked(LINK_SET, 0, "", "fw_done");
}

default
{
    state_entry()
    {
        gPrimCount = llGetObjectPrimCount(llGetKey())+llGetNumberOfPrims()*!!llGetAttached();
        
        integer link = gPrimCount;
        integer col = -1;
        integer row = -1;
        integer i;
        integer j;
        list    items;
        
        do
        {
            items = llParseString2List(llGetLinkName(link), [ ":" ], []);
            if (llList2String(items, 0) == "FURWARE text mesh")
            {
                if (llGetLinkNumberOfSides(link) == 8)
                {
                    if (llList2Integer(items, 2) > row) row = llList2Integer(items, 2);
                    if (llList2Integer(items, 3) > col) col = llList2Integer(items, 3);
                }
                else
                {
                    llOwnerSay("Only 8 face prims are supported.");
                    return;
                }
            }
        }
        while (--link > 0);
        
        if (col < 0 || row < 0)
        {
            llOwnerSay("No text prims found");
            return;
        }
        
        gCols = ++col * 8;
        gColPrims = col;
        gRows = ++row;
        gTextLinks = [];
        link = gPrimCount;
        
        for (i = 0; i < row * col; ++i) gTextLinks += 0;
        
        do
        {
            items = llParseString2List(llGetLinkName(link), [ ":" ], []);
            if (llList2String(items, 0) == "FURWARE text mesh")
            {
                i = llList2Integer(items, 2) * col + llList2Integer(items, 3);
                gTextLinks = llListReplaceList(gTextLinks, [ link ], i, i);
            }
        }
        while (--link >= 0);
        
        for (i = 0; i < 256; ++i)
        {
            j = llListFindList(gCodePageUUIDs, [ i ]);
            if (~j) gCodePages += llList2String(gCodePageUUIDs, j + 1);
            else    gCodePages += gBlankUUID;
        }
        gCodePageUUIDs = []; // No longer needed, empty it to free up memory?
        gASCII_UUID = llList2String(gCodePages, 0);
        gSpaceOffset = <-gOffset.x, gOffset.y - 2 * 0.0625, 0.0>;
		
        Flush(); // Invalidate the whole display
        Draw("");
        
        llMessageLinked(LINK_SET, 0, "", "fw_ready");
    }
    
    link_message(integer source, integer num, string str, key id)
    {
        if (llGetSubString(id, 0, 2) != "fw_") return;
        
        list    commands = llParseString2List(id, [ ":" ], []);
        string  command  = llToLower(llStringTrim(llList2String(commands, 0), STRING_TRIM));
        list    options;
        list    params;
        string  param;
        string  value_lc;
        integer i;
        integer pos;
        
        if      (command == "fw_data")
        {
            Draw(str);
        }
        else if (command == "fw_direct")
        {
            Render(UTFtoInt(str), llList2Integer(commands, 1),llList2Integer(commands, 2), TRUE);
        }
        else if (command == "fw_conf" || command == "fw_defaultconf")
        {
            options = llParseString2List(str, [ ";" ], []);
            
            for(i = 0; i < llGetListLength(options); ++i)
            {
                params = llParseString2List(llList2String(options, i), [ "=" ], []);
                
                if (llGetListLength(params) > 1)
                {
                    param = llToLower(llStringTrim(llList2String(params, 0), STRING_TRIM));
                    value_lc = llToLower(llStringTrim(llList2String(params, 1), STRING_TRIM));
                    
                    if (param == "c")
                    {
                        pos = llListFindList(gColors, [ value_lc ]);
                        
                        if (pos == 0)
                        {
                        	gColor = <llFrand(1.0), llFrand(1.0), llFrand(1.0), 1.0>;
                        }
                        else if (pos % 2 == 0)
                        {
                            gColor = llList2Rot(gColors, pos + 1);
                        }
                        else
                        {
                            value_lc = "<" + value_lc + ">";
                            gColor = (rotation)value_lc;
                            if (gColor == ZERO_ROTATION)
                            {
                                vector tmp = (vector)value_lc;
                                gColor = <tmp.x, tmp.y, tmp.z, 1.0>;
                            }
                        }
                    }
                    else if (param == "w")
                    {
                        pos = llListFindList([ "none", "char", "word" ], [ value_lc ]);
                        if (~pos)
                        {
                            if (gWrap != pos) Flush();
                            gWrap = pos;
                        }
                    }
                    else if (param == "a")
                    {
                        pos = llListFindList([ "left", "center", "right" ], [ value_lc ]);
                        if (~pos)
                        {
                            if (gAlign != pos) Flush();
                            gAlign = pos;
                        }
                    }
                    else if (param == "t")
                    {
                        pos = llListFindList([ "off", "on" ], [ value_lc ]);
                        if (~pos)
                        {
                            if (gTrim != pos) Flush();
                            gTrim = pos;
                        }
                    }
                    else if (param == "force")
                    {
                        pos = llListFindList([ "off", "on" ], [ value_lc ]);
                        if (~pos) gForce = pos;
                    }
                } // more than 1 param
            } // iterate options
        }
        else if (command == "fw_memory")
        {
            llOwnerSay("HexText Free Mem: " + (string)llGetFreeMemory());
        }
        else if (command == "fw_notify")
        {
            gNotify = (llToLower(llStringTrim(str, STRING_TRIM)) == "on");
        }
        else if (command == "fw_reset")
        {
            llResetScript();
        }
    }
    
    changed(integer change)
    {
        // Must reset, only time we search for link names.
        if (change & CHANGED_LINK &&
            gPrimCount != llGetObjectPrimCount(llGetKey())+llGetNumberOfPrims()*!!llGetAttached())
        {
            llResetScript();
        }
    }
}