<< Back to Code Snippets & Analyses

Code Snippet & Analysis #001

///ULTIMATE TRINITY SCRIPT

//this fills txtLinesWBreaks[w] & txtULBlanks[w] && gets position for underlining (AND DOES SO FOR NO-SPACES AND FOR LARGE FONT WITHOUT CRASHING)

//and it accommodates the new Titles-With-Blanks Engine ™


var i, w, pos_space, pos_current, text_current, text_output, text_full, text_full2, fat_storage, num_of_spaces, num_of_spaces2, pos_space2, pos_current2, p, txtULBlanksSpacelessTest, pos_currentBlankCheck;

    pos_space = -1;

    pos_current = 1;

    w = 1;

    text_full = “”;

    text_full2 = “”;

    fat_storage = “”;

    num_of_spaces = 0;

    num_of_spaces2 = 0;

    pos_space2 = -1;

    pos_current2 = 1;

    p = 0;

    txtULBlanksSpacelessTest = “”;

    pos_currentBlankCheck = 0;

if textUpdated = 1

    {

    instance_destroy(obj_assembledBlank);

    maxStringHeight = string_height(global.stringForHeight)*global.fontScaleReadLib;

    //numReadLibLinesDisplayed

    //imposedBreakLineMax (not global)

    //scrolledPageReadLib

    //maxPageScrollReadLib

    

    ///10-15-19///SPECIAL NEW: TITLE, EQUIPPED FOR BLANKS

    //All I did was change i=1 to i=0 for the main for loop below here.

    /////…That was wonky. Let’s try it anyway.

    //Here goes.

    //These are all being declared here for the first time. No expected issued with that yet.

    txtTitleReadLib = string_replace_all(txtLinesAll[0], “=========”, “”);

    txtTitleReadLib = string_replace_all(txtTitleReadLib, “+++++++++”, “”);

    txtTitleReadLibOG = txtTitleReadLib;

    txtTitleReadLib2 = “”;

    txtULTitle = txtLinesAll[0];

    fontScaleReadLibNarrow = 1;

    posCurrentTitle = 1;

    posSpaceTitle[0] = -1;

    sizeDifference[0] = 0;

    isSpaceTitle = 0;

    numSpacesTitle = 0;

    leastPositiveDifference = string_width(txtTitleReadLib)*global.fontScaleReadLibTitle;

    leastNegativeDifference = string_width(txtTitleReadLib)*global.fontScaleReadLibTitle;

    

    

    if string_width(txtTitleReadLib)*global.fontScaleReadLibTitle > global.readLibTitleBoxWidth //if it does NOT just plain fit, then…

        {

        //Plan A:

        //We squash it until a certain ratio

        //If this does not work…

        while string_width(txtTitleReadLib)*global.fontScaleReadLibTitle*fontScaleReadLibNarrow > global.readLibTitleBoxWidth && fontScaleReadLibNarrow >= 0.72

            {

            fontScaleReadLibNarrow -= 0.02;

            }

        

        //Plan B REDO: (This IF contains Plans B, C, and D)

        //We UNSQUASH it and linebreak at the halfway point, favoring bottom-as-longer

        //First: Go through and find all positions of spaces

        //Second: Break at the different spaces to find leastPositive and leastNegative

        //Third: Use leastPositive UNLESS it is a certain ratio bigger than leastNegative

        if string_width(txtTitleReadLib)*global.fontScaleReadLibTitle*fontScaleReadLibNarrow > global.readLibTitleBoxWidth // && if the ratios work out to permit 2 lines in title

            {

            fontScaleReadLibNarrow = 1;

            //This while loop: Get position of all spaces in txtTitleReadLib

            while (string_length(txtTitleReadLib) >= posCurrentTitle)

                {

                if (string_char_at(txtTitleReadLib,posCurrentTitle) == ” “)

                    {

                    posSpaceTitle[numSpacesTitle] = posCurrentTitle;

                    numSpacesTitle += 1;

                    isSpaceTitle = 1;

                    }

                posCurrentTitle += 1;

                }

            

            //first, here’s what we’ll do if there were no spaces: (READ COMMENTS BELOW — SPACELESS SEEMS INCOMPLETE)

            var gonnaUseSpaceless = 0;

            if isSpaceTitle == 0

                {

                //go character by character, looking for the closest ratio. cut when it starts to get bigger.

                var spacelessRatio = 0;

                for (i = 1; i <= string_length(txtTitleReadLib); i++)

                    {

                    txtSpacelessTop = string_copy(txtTitleReadLib, 1, i) + “-“;

                    txtSpacelessBottom = string_delete(txtTitleReadLib, 1, i);

                    spacelessRatio = (string_width(txtSpacelessBottom)*global.fontScaleReadLibTitle)/(string_width(txtSpacelessTop)*global.fontScaleReadLibTitle);

                    if spacelessRatio < 1

                        {

                        //now we check: if both lines don’t fit! (then we know the whole thing is TOO LONG to fit)

                        if string_width(txtSpacelessBottom)*global.fontScaleReadLibTitle > global.readLibTitleBoxWidth && string_width(txtSpacelessTop)*global.fontScaleReadLibTitle*fontScaleReadLibNarrow > global.readLibTitleBoxWidth

                            {

                            while fontScaleReadLibNarrow >= 0.72 && string_width(txtSpacelessBottom)*global.fontScaleReadLibTitle*fontScaleReadLibNarrow > global.readLibTitleBoxWidth && string_width(txtSpacelessTop)*global.fontScaleReadLibTitle*fontScaleReadLibNarrow > global.readLibTitleBoxWidth

                                {

                                fontScaleReadLibNarrow -= 0.02;

                                }

                            //if it still doesn’t fit after full narrowing (0.7)

                            if string_width(txtSpacelessBottom)*global.fontScaleReadLibTitle*fontScaleReadLibNarrow > global.readLibTitleBoxWidth && string_width(txtSpacelessTop)*global.fontScaleReadLibTitle*fontScaleReadLibNarrow > global.readLibTitleBoxWidth //aka it didn’t fit

                                {

                                //we’re gonna have to manually max the topline + “-” and the bottom line + “…”

                                var q = 1;

                                while (q <= string_length(txtTitleReadLibOG)) && gonnaUseSpaceless == 0

                                    {

                                    txtSpacelessTop = string_copy(txtTitleReadLibOG, 1, q);

                                    if string_width(txtSpacelessTop)*global.fontScaleReadLibTitle*fontScaleReadLibNarrow + string_width(“-“)*global.fontScaleReadLibTitle*fontScaleReadLibNarrow > global.readLibTitleBoxWidth && q >= 2

                                        {

                                        gonnaUseSpaceless = 1;

                                        txtTitleReadLib = string_copy(txtTitleReadLibOG, 1, q-1) + “-“;

                                        txtTitleReadLib2 = string_delete(txtTitleReadLibOG, 1, string_length(txtTitleReadLib));

                                        if string_width(txtTitleReadLib2)*global.fontScaleReadLibTitle*fontScaleReadLibNarrow > global.readLibTitleBoxWidth

                                            {

                                            var k = 1, bottomTrulyFilled = 0;

                                            while (k <= string_length(txtTitleReadLib2) && bottomTrulyFilled == 0)

                                                {

                                                txtSpacelessBottom = string_copy(txtTitleReadLib2, 1, k);

                                                if string_width(txtSpacelessBottom)*global.fontScaleReadLibTitle*fontScaleReadLibNarrow + string_width(“…”)*global.fontScaleReadLibTitle*fontScaleReadLibNarrow > global.readLibTitleBoxWidth && k >= 2

                                                    {

                                                    bottomTrulyFilled = 1;

                                                    txtTitleReadLib2 = string_copy(txtTitleReadLib2, 1, k-1) + “…”;

                                                    }

                                                else if k == string_length(txtTitleReadLib2) //just a strange failsafe to fill txtTitleReadLib. Logically, this should be an impossible scenario

                                                    {

                                                    txtTitleReadLib2 = “”;

                                                    }

                                                k++;

                                                }

                                            }

                                        }

                                    else if q == string_length(txtTitleReadLibOG) //just a strange failsafe to fill txtTitleReadLib. Logically, this should be an impossible scenario

                                        {

                                        txtTitleReadLib = txtTitleReadLibOG;

                                        txtTitleReadLib2 = “”;

                                        }

                                    q++;

                                    }

                                break;

                                }

                            else //aka it did fit at some point in the Narrowing (technically including Narrow == 1, but logically only Narrow == 0.98)

                                {

                                txtTitleReadLib = txtSpacelessTop;

                                txtTitleReadLib2 = txtSpacelessBottom;

                                gonnaUseSpaceless = 1;

                                break;

                                }

                            }

                        else //AKA, if string_width(txtSpacelessBottom)*global.fontScaleReadLibTitle <= global.readLibTitleBoxWidth

                            {

                            txtTitleReadLib = txtSpacelessTop;

                            txtTitleReadLib2 = txtSpacelessBottom;

                            gonnaUseSpaceless = 1;

                            break;

                            }

                        }

                    else if i == string_length(txtTitleReadLib)

                        {

                        gonnaUseSpaceless = 0;

                        //don’t actually have to do anything here, I think. I bet I could delete this “if” and nothing would break ever.

                        //this only occurs in a case where there are no spaces, and the bottom can never be longer than the top.

                        //which i believe would only happen if the name was two characters or fewer, and in that case,

                        //txtReadLib would stay what it is already set to be.

                        }

                    }

                //******************************************************

                //something here looks wrong to me. Should the below “while” function on an || between ReadLib 1 and 2?

                //And what if a spaceless lib is just too long? like imagine way too long? this doesn’t work. It’d have to narrow

                //the whole thing (which it does here?), and then if that failed, do the top-line, already-narrow method

                while string_width(txtTitleReadLib)*global.fontScaleReadLibTitle*fontScaleReadLibNarrow > global.readLibTitleBoxWidth && string_width(txtTitleReadLib)*global.fontScaleReadLibTitle*fontScaleReadLibNarrow > global.readLibTitleBoxWidth && fontScaleReadLibNarrow >= 0.72

                    {

                    fontScaleReadLibNarrow -= 0.02;

                    }

                }

            

            //This big while loop: Investigates the spaces. Concludes with the positions of spaces

            //for leastPositive (bottom is bigger) and leastNegative (top is bigger)

            //Also has the values for these “leasts” to see how disparate the lines are in length

            

            var i = 0;

            while isSpaceTitle == 1 && (i < numSpacesTitle) && (leastNegativeDifference >= string_width(txtTitleReadLib)*global.fontScaleReadLibTitle)

                {

                testTopString = string_copy(txtTitleReadLib,1,posSpaceTitle[i]);

                testBottomString = string_copy(txtTitleReadLib,posSpaceTitle[i],string_length(txtTitleReadLib)-posSpaceTitle[i]);

                sizeDifference[i] = (string_width(testBottomString)*global.fontScaleReadLibTitle)-(string_width(testTopString)*global.fontScaleReadLibTitle);

                if i == 0

                    {

                    leastDifference = sizeDifference[i];

                    //this conditional below covers the situation of only ONE space in the title. Is it well-formed?

                    if numSpacesTitle == 1

                        {

                        if leastDifference >= 0

                            {

                            leastPositiveDifference = leastDifference;

                            posLeastPositiveDifference = posSpaceTitle[0];

                            }

                        else if leastDifference < 0

                            {

                            leastNegativeDifference = leastDifference;

                            posLeastNegativeDifference = posSpaceTitle[0];

                            }

                        }

                    }

                else if i == 1 //meaning, if there are 2 spaces in the string

                    {

                    if leastDifference >= 0

                        {

                        if sizeDifference[i] >= 0

                            {

                            leastPositiveDifference = sizeDifference[i];

                            posLeastPositiveDifference = posSpaceTitle[i];

                            }

                        else if sizeDifference[i] < 0

                            {

                            leastPositiveDifference = leastDifference;

                            posLeastPositiveDifference = posSpaceTitle[0];

                            //debug attempt vv untested results as of 10-16-19

                            leastNegativeDifference = sizeDifference[i];

                            posLeastNegativeDifference = posSpaceTitle[i];

                            }

                        }

                    else if leastDifference < 0

                        {

                        leastNegativeDifference = leastDifference;

                        posLeastNegativeDifference = posSpaceTitle[0];

                        }

                    }

                else if i >= 2 //meaning, if there are 3 or more spaces in the string

                    {

                    if sizeDifference[i] >= 0

                        {

                        leastPositiveDifference = sizeDifference[i];

                        posLeastPositiveDifference = posSpaceTitle[i];

                        }

                    else if sizeDifference[i] < 0

                        {

                        leastNegativeDifference = sizeDifference[i];

                        posLeastNegativeDifference = posSpaceTitle[i];

                        }

                    }

                i++;

                }

            

            //Now we have our info about line widths… Time to see if we’re gonna use them

            //THE BELOW LOGIC SEEMS REALLY GOOD AND CLEAN TO ME

            var gonnaUse = -1, posRatio = 0, negRatio = 0;

            if isSpaceTitle == 1

                {

                if leastPositiveDifference < string_width(txtTitleReadLib)*global.fontScaleReadLibTitle

                    {

                    testPosLinesTopString = string_copy(txtTitleReadLib,1,posLeastPositiveDifference);

                    testPosLinesBottomString = string_copy(txtTitleReadLib,posLeastPositiveDifference,string_length(txtTitleReadLib)-posLeastPositiveDifference);

                    //bottom is equal to or longer than top, so top/bottom is LESS THAN (or equal to) ONE (aka this value is less (or equal to) than one)

                    posRatio = (string_width(testPosLinesTopString)*global.fontScaleReadLibTitle)/(string_width(testPosLinesBottomString)*global.fontScaleReadLibTitle);

                    //so posRatio IS the ratio of bottom:top when bottom is longer

                    //negRatio IS the ratio of top:bottom when top is longer

                    //so we want to use posRatio when possible, and make the bottom longer

                    //but we’ll use negRatio if it’s just a lot better in terms of equal line-lengths (aka it’s much closer to 1)

                    }

                if leastNegativeDifference < string_width(txtTitleReadLib)*global.fontScaleReadLibTitle

                //if leastNegativeDifference < 

                    {

                    testNegLinesTopString = string_copy(txtTitleReadLib,1,posLeastNegativeDifference);

                    testNegLinesBottomString = string_copy(txtTitleReadLib,posLeastNegativeDifference,string_length(txtTitleReadLib)-posLeastNegativeDifference);

                    //top is longer than bottom, so bottom/top is LESS THAN ONE (aka this value is less than one)

                    negRatio = (string_width(testNegLinesBottomString)*global.fontScaleReadLibTitle)/(string_width(testNegLinesTopString)*global.fontScaleReadLibTitle);

                    }

                if negRatio == 0 //if the top line was never longer than the bottom…how would this event happen?

                    {

                    if string_width(testPosLinesBottomString)*global.fontScaleReadLibTitle <= global.readLibTitleBoxWidth

                        {

                        txtTitleReadLib = testPosLinesTopString;

                        txtTitleReadLib2 = testPosLinesBottomString;

                        gonnaUse = 1;

                        }

                    else {gonnaUse = 0} // redundant code? bad even? might cover fringe cases of stuff

                    }

                else if posRatio == 0 //if the bottom line was never equal to or longer than the top

                    {

                    if string_width(testNegLinesTopString)*global.fontScaleReadLibTitle <= global.readLibTitleBoxWidth

                        {

                        txtTitleReadLib = testNegLinesTopString;

                        txtTitleReadLib2 = testNegLinesBottomString;

                        gonnaUse = 1;

                        }

                    else {gonnaUse = 0} // redundant code? bad even? might cover fringe cases of stuff

                    }

                else if posRatio >= (negRatio + (1-negRatio)/2)

                    {

                    if string_width(testPosLinesBottomString)*global.fontScaleReadLibTitle <= global.readLibTitleBoxWidth

                        {

                        txtTitleReadLib = testPosLinesTopString;

                        txtTitleReadLib2 = testPosLinesBottomString;

                        gonnaUse = 1;

                        }

                    else if string_width(testNegLinesTopString)*global.fontScaleReadLibTitle <= global.readLibTitleBoxWidth

                        {

                        txtTitleReadLib = testNegLinesTopString;

                        txtTitleReadLib2 = testNegLinesBottomString;

                        gonnaUse = 1;

                        }

                    else {gonnaUse = 0} // redundant code? bad even? might cover fringe cases of stuff

                    }

                else if (negRatio + (1-negRatio)/2) > posRatio

                    {

                    if string_width(testNegLinesTopString)*global.fontScaleReadLibTitle <= global.readLibTitleBoxWidth

                        {

                        txtTitleReadLib = testNegLinesTopString;

                        txtTitleReadLib2 = testNegLinesBottomString;

                        gonnaUse = 1;

                        }

                    else if string_width(testPosLinesBottomString)*global.fontScaleReadLibTitle <= global.readLibTitleBoxWidth

                        {

                        txtTitleReadLib = testPosLinesTopString;

                        txtTitleReadLib2 = testPosLinesBottomString;

                        gonnaUse = 1;

                        }

                    else {gonnaUse = 0} // redundant code? bad even? might cover fringe cases of stuff

                    }

                else {gonnaUse = 0}

                } //end Plan B & C

               

                

                ///COMBTHROUGH AT THE END OF THE DAY, 10-15-19 to 10-16-19 (5:43am)

                ///I really think the logic down to this point works!!!

                ///Except for SPACELESS. I need to refine that.

                ///And maybe double-check scenarios in my head (e.g., one space, two space, very asymmetrical spaces)

                ///But I think this is all really good…

                ///Flaws must be below.

                 

            //Plan D Lite: (original Plan D too daunting; in comments below)

            //Loop:

            //We squash it one iota, then linebreak at the halfway point (to be redetermined)

            //Then we check if it worked. If it did not work, round the linebreak the other direction.

            //If that did not work…

            //We UNbreak it, squash it another iota, then repeat — redetermining the linebreak each time (unless I realize that’s dumb)

            //We do this until a certain ratio

            //:End Loop

            //If this does not work…

            

            while isSpaceTitle == 1 && gonnaUse == 0 && fontScaleReadLibNarrow >= .72

                {

                fontScaleReadLibNarrow -= .02;

                if negRatio == 0

                    {

                    if string_width(testPosLinesBottomString)*global.fontScaleReadLibTitle*fontScaleReadLibNarrow <= global.readLibTitleBoxWidth

                        {

                        txtTitleReadLib = testPosLinesTopString;

                        txtTitleReadLib2 = testPosLinesBottomString;

                        gonnaUse = 1;

                        }

                    }

                else if posRatio == 0

                    {

                    if string_width(testNegLinesTopString)*global.fontScaleReadLibTitle*fontScaleReadLibNarrow <= global.readLibTitleBoxWidth

                        {

                        txtTitleReadLib = testNegLinesTopString;

                        txtTitleReadLib2 = testNegLinesBottomString;

                        gonnaUse = 1;

                        }

                    }

                else if posRatio >= (negRatio + (1-negRatio)/2)

                    {

                    if string_width(testPosLinesBottomString)*global.fontScaleReadLibTitle*fontScaleReadLibNarrow <= global.readLibTitleBoxWidth

                        {

                        txtTitleReadLib = testPosLinesTopString;

                        txtTitleReadLib2 = testPosLinesBottomString;

                        gonnaUse = 1;

                        }

                    else if string_width(testNegLinesTopString)*global.fontScaleReadLibTitle*fontScaleReadLibNarrow <= global.readLibTitleBoxWidth

                        {

                        txtTitleReadLib = testNegLinesTopString;

                        txtTitleReadLib2 = testNegLinesBottomString;

                        gonnaUse = 1;

                        }

                    }

                else if (negRatio + (1-negRatio)/2) > posRatio

                    {

                    if string_width(testNegLinesTopString)*global.fontScaleReadLibTitle*fontScaleReadLibNarrow <= global.readLibTitleBoxWidth

                        {

                        txtTitleReadLib = testNegLinesTopString;

                        txtTitleReadLib2 = testNegLinesBottomString;

                        gonnaUse = 1;

                        }

                    else if string_width(testPosLinesBottomString)*global.fontScaleReadLibTitle*fontScaleReadLibNarrow <= global.readLibTitleBoxWidth

                        {

                        txtTitleReadLib = testPosLinesTopString;

                        txtTitleReadLib2 = testPosLinesBottomString;

                        gonnaUse = 1;

                        }

                    }

                //else {gonnaUse = 0} unnecessary line; it’d already be 0 in this case

                

                ////////////////////

                //This looks good to me! I can’t see the problem thus far.

                //Problems must be below…

                /////////////////

                

                } //if this STILL doesn’t work, then we have to pick whichever space location makes top line as long as possible at .72 without leaving boundaries.

            

            //this will find the longest top line possible, bottom will be what remains after that, and then “…” as is presumably needed.

            var j = 0;

            while isSpaceTitle == 1 && gonnaUse == 0 && (j < numSpacesTitle) // && fontScaleReadLibNarrow == .7

                {

                testTopString = string_copy(txtTitleReadLibOG,1,posSpaceTitle[j]);

                //This entire if: If the first space in the title occurs so late that the top line overflows to the next line before reaching it

                //Frankly this must be an impossible situation, but I’m not sure yet. (maybe i should test it with a long-before-space title)

                if string_width(testTopString)*global.fontScaleReadLibTitle*fontScaleReadLibNarrow > global.readLibTitleBoxWidth && j == 0

                    {

                    //break it at the furthest character possible (maybe insert hyphen)

                    var i = 1;

                    while (i <= string_length(txtTitleReadLibOG)) && gonnaUse == 0

                        {

                        txtSpacelessTop = string_copy(txtTitleReadLibOG, 1, i);

                        if string_width(txtSpacelessTop)*global.fontScaleReadLibTitle*fontScaleReadLibNarrow + string_width(“-“)*global.fontScaleReadLibTitle*fontScaleReadLibNarrow > global.readLibTitleBoxWidth && i >= 2

                            {

                            gonnaUse = 1;

                            txtTitleReadLib = string_copy(txtTitleReadLibOG, 1, i-1) + “-“;

                            txtTitleReadLib2 = string_delete(txtTitleReadLibOG, 1, string_length(txtTitleReadLib));

                            if string_width(txtTitleReadLib2)*global.fontScaleReadLibTitle*fontScaleReadLibNarrow > global.readLibTitleBoxWidth

                                {

                                var k = 1, bottomTrulyFilled = 0;

                                while (k <= string_length(txtTitleReadLib2) && bottomTrulyFilled == 0)

                                    {

                                    txtSpacelessBottom = string_copy(txtTitleReadLib2, 1, k);

                                    if string_width(txtSpacelessBottom)*global.fontScaleReadLibTitle*fontScaleReadLibNarrow + string_width(“…”)*global.fontScaleReadLibTitle*fontScaleReadLibNarrow > global.readLibTitleBoxWidth && k >= 2

                                        {

                                        bottomTrulyFilled = 1;

                                        txtTitleReadLib2 = string_copy(txtTitleReadLib2, 1, k-1) + “…”;

                                        }

                                    else if k == string_length(txtTitleReadLib2) //just a strange failsafe to fill txtTitleReadLib. Logicaly, this should be an impossible scenario

                                        {

                                        txtTitleReadLib2 = “”;

                                        }

                                    k++;

                                    }

                                }

                            }

                        else if i == string_length(txtTitleReadLibOG) //just a strange failsafe to fill txtTitleReadLib. Logicaly, this should be an impossible scenario

                            {

                            txtTitleReadLib = txtTitleReadLibOG;

                            txtTitleReadLib2 = “”;

                            }

                        i++;

                        }

                    }

                //if the first space in the title does not occur too late, but a LATER SPACE DOES cause top to overflow

                else if string_width(testTopString)*global.fontScaleReadLibTitle*fontScaleReadLibNarrow > global.readLibTitleBoxWidth && j >= 1

                    {

                    //break it at the space previous to this one

                    gonnaUse = 1;

                    txtTitleReadLib = string_copy(txtTitleReadLibOG, 1, posSpaceTitle[j-1]);

                    txtTitleReadLib2 = string_delete(txtTitleReadLibOG, 1, string_length(txtTitleReadLib));

                    if string_width(txtTitleReadLib2)*global.fontScaleReadLibTitle*fontScaleReadLibNarrow > global.readLibTitleBoxWidth

                        {

                        var k = 1, bottomTrulyFilled = 0;

                        while (k <= string_length(txtTitleReadLib2) && bottomTrulyFilled == 0) // this loops until we went through

                            {

                            txtSpacedBottom = string_copy(txtTitleReadLib2, 1, k);

                            if string_width(txtSpacedBottom)*global.fontScaleReadLibTitle*fontScaleReadLibNarrow + string_width(“…”)*global.fontScaleReadLibTitle*fontScaleReadLibNarrow > global.readLibTitleBoxWidth && k >= 2

                                {

                                bottomTrulyFilled = 1;

                                txtTitleReadLib2 = string_copy(txtTitleReadLib2, 1, k-1) + “…”;

                                }

                            else if k == string_length(txtTitleReadLib2) //just a strange failsafe to fill txtTitleReadLib. Logicaly, this should be an impossible scenario

                                {

                                txtTitleReadLib2 = “”;

                                }

                            k++;

                            }

                        }

                    }

                //at this point, the only thing that fits this criteria is a title with at least one space, the last space

                //of which would STILL appear on the top line (as there is no negative ratio)

                else if j == numSpacesTitle – 1

                    {

                    gonnaUse = 1;

                    txtTitleReadLib = string_copy(txtTitleReadLibOG, 1, posSpaceTitle[j]);

                    txtTitleReadLib2 = string_delete(txtTitleReadLibOG, 1, string_length(txtTitleReadLib));

                    if string_width(txtTitleReadLib2)*global.fontScaleReadLibTitle*fontScaleReadLibNarrow > global.readLibTitleBoxWidth

                        {

                        var fina = 1, bottomTrulyFilled = 0;

                        while (fina <= string_length(txtTitleReadLib2) && bottomTrulyFilled == 0) // this loops until we went through

                            {

                            txtSpacedBottom = string_copy(txtTitleReadLib2, 1, fina);

                            if string_width(txtSpacedBottom)*global.fontScaleReadLibTitle*fontScaleReadLibNarrow + string_width(“…”)*global.fontScaleReadLibTitle*fontScaleReadLibNarrow > global.readLibTitleBoxWidth && fina >= 2

                                {

                                bottomTrulyFilled = 1;

                                txtTitleReadLib2 = string_copy(txtTitleReadLib2, 1, fina-1) + “…”;

                                }

                            else if fina == string_length(txtTitleReadLib2) //just a strange failsafe to fill txtTitleReadLib. Logicaly, this should be an impossible scenario

                                {

                                txtTitleReadLib2 = “”;

                                }

                            fina++;

                            }

                        }

                    }

                j++;

                }   

            }//ends all the 2-line checks, Plans B-D, and even ends the “…”-adding, which would be Plan E

        }

        

    ///////////////////////////////////////////////////

    ///////////////////////////////////////////////////

    ///////////////////////////////////////////////////

    

    

    for (i = 1; i <= lineMax; i+= 1)

        {

        txtLinesWBreaks[w] = txtLinesAll[i];

        

        txtLinesWBreaks[w] = string_replace_all(txtLinesWBreaks[w],”=========”,””);

        txtLinesWBreaks[w] = string_replace_all(txtLinesWBreaks[w],”+++++++++”,””);

        txtULBlanks[w] = txtLinesAll[i];


        while (string_length(txtLinesWBreaks[w]) >= pos_current)

            {

            if ((string_width(string_copy(txtLinesWBreaks[w],1,pos_current))*global.fontScaleReadLib) > global.readLibBoundingBoxWidth)

                {

                if (pos_space != -1)

                    {

                    text_full = txtLinesWBreaks[w];

                    txtLinesWBreaks[w] = string_copy(txtLinesWBreaks[w],1,pos_space);

                    

                    

                    //do this below after incrementing w

                    //txtLinesWBreaks[w+1] = string_copy(text_full,pos_space+1,string_length(text_full)-(pos_space));

                    

                    //THE BELOW “IF” STATEMENT CRASHES CODE WHEN FONT IS TOO BIG. BECAUSE NO SPACE??? It tried to index txtULBlanks[0,22] when it’s only [1,22]

                    //(new) here, we will break the “blanked” line at the same place as the unblanked line

                    if (string_pos(“=========”, txtULBlanks[w]) != 0 || string_pos(“+++++++++”, txtULBlanks[w]) != 0)

                        {

                        while (num_of_spaces != num_of_spaces2)

                            {

                            if (string_char_at(txtULBlanks[w],pos_current2) == ” “)

                                {

                                pos_space2 = pos_current2;

                                num_of_spaces2 += 1;

                                }

                            pos_current2 += 1;

                            }

                        text_full2 = txtULBlanks[w];

                        txtULBlanks[w] = string_copy(txtULBlanks[w],1,pos_space2);

                        

                        

                        //////////////// trying… nah not going with this for now. didn’t seem to help

                        //txtULBlanks[w+1] = string_copy(text_full2,pos_space2+1,string_length(text_full2)-(pos_space2));

                        ////////////////

                        }

                    else

                        {

                        txtULBlanks[w] = txtLinesWBreaks[w];

                        text_full2 = txtULBlanks[w];

                        

                        /////

                        //txtULBlanks[w+1] = txtLinesWBreaks[w+1];

                        /////

                        }

                    // (end new)

                    w += 1;

                    txtLinesWBreaks[w] = string_copy(text_full,pos_space+1,string_length(text_full)-(pos_space));

                    //the above line: removes the text we just looked at from the current text string

                    

                    

                    ////(new) here, we will create the new line by removing the text we just saved as the previous line

                    ////////////////////////////

                    

                    ///THIS HAS BEEN GOOD FOR ME IN THE PAST, BUT I’M TINKERING WITH IT NOW (9-24-19)

                    if (string_pos(“=========”, text_full2) != 0 || string_pos(“+++++++++”, text_full2) != 0)

                        {

                        txtULBlanks[w] = string_copy(text_full2,pos_space2+1,string_length(text_full2)-(pos_space2));

                        }

                    else {txtULBlanks[w] = txtLinesWBreaks[w]};

                    

                    //////////////////////////////////////////////////////////////////////

                    

                    // (end new)

                    

                    pos_current = 1;

                    pos_space = -1;

                    

                    //(new)

                    pos_current2 = 1;

                    pos_space2 = -1;

                    

                    num_of_spaces = 0;

                    num_of_spaces2 = 0;

                    // (end new)

                    }

                    

                //Still need to code UNDERLINE PREPPING in this case — that there is no space in the line

                //

                //

                /////////////////////////////////////////////////////////////////////////////////////////////

                // LET’S MAKE IT HAPPEN!

                else if (pos_space == -1)

                    {

                    //if not, we can force line breaks

                    text_full = txtLinesWBreaks[w];

                    //remove the text we just looked at from the current text string

                    txtLinesWBreaks[w] = string_copy(txtLinesWBreaks[w],1,pos_current-1);

                    

                    

                    

                    ///here fucking goes. ULBlanks time…

                    //Make these variables exist up top. haven’t initialized em yet

                    ULBlankLengthCheck = string_length(txtLinesWBreaks[w]) + string_length(“=========”);

                    txtULBlanksSpacelessTest = string_copy(txtULBlanks[w],1,ULBlankLengthCheck);

                    

                    pos_currentBlankCheck = pos_current-1;

                    

                    while string_pos(“=========”,txtULBlanksSpacelessTest) > 0

                        {

                        txtULBlanksSpacelessTest = string_replace(txtULBlanksSpacelessTest,”=========”,””);

                        pos_currentBlankCheck = pos_currentBlankCheck + string_length(“=========”);

                        }

                    

                    while string_pos(“+++++++++”,txtULBlanksSpacelessTest) > 0

                        {

                        txtULBlanksSpacelessTest = string_replace(txtULBlanksSpacelessTest,”+++++++++”,””);

                        pos_currentBlankCheck = pos_currentBlankCheck + string_length(“=========”);

                        }

                        

                    text_full2 = txtULBlanks[w];

                    txtULBlanks[w] = string_copy(txtULBlanks[w],1,pos_currentBlankCheck);

                    

                    ///end of crazy experiment 9-23-19

                    

                    w += 1;

                    txtLinesWBreaks[w] = string_copy(text_full,pos_current,string_length(text_full)-(pos_current-1));

                    //crazy experiment again

                    txtULBlanks[w] = string_copy(text_full2,pos_currentBlankCheck+1,string_length(text_full2)-(pos_currentBlankCheck));

                    

                    pos_current = 1;

                    pos_space = -1;

                    num_of_spaces = 0;

                    }

                }

            if (string_char_at(txtLinesWBreaks[w],pos_current) == ” “)

                {

                pos_space = pos_current;

                num_of_spaces += 1;

                }

                

            /* nah this was dumb shit. delete it

            else if (string_char_at(txtLinesWBreaks[w],pos_current) == “=”)

                {

                consec_equals += 1;

                }

            else if (string_char_at(txtLinesWBreaks[w],pos_current) == “+”)

                {

                consec_pluses += 1;

                }

            */

            pos_current += 1;

            

            

            }

            

        if (string_length(txtLinesWBreaks[w]) > 0) w += 1;

        breakLineMax = w;

        //Gotta make page scroll now. it’s based on breakLineMax, hence we do it here

        maxPageScrollReadLib = breakLineMax/numReadLibLinesDisplayed;

        

        //9-24-19 will this solve it…

        pos_current = 1;

        pos_space = -1;

        

        //(new)

        pos_current2 = 1;

        pos_space2 = -1;

        

        num_of_spaces = 0;

        num_of_spaces2 = 0;

        }

        

        

        

        

        

        

    //GOD NEW, ALL BELOW THIS POINT

    /*

    var q = 0;

    var storage = “”;

    var storage2 = “”;

    var d_at = 0

    var d_at2 = 0

    for (i = 1; i < breakLineMax; i++)

        {

        q = 1;

        stringsInLine[i] = q;

        blankLocater[i,q] = txtULBlanks[i];

        d_at = string_pos(“=========”, blankLocater[i,q]);

        while(d_at > 0)

            {

            storage = blankLocater[i,q];

            blankLocater[i,q] = string_delete(blankLocater[i,q], d_at, string_length(blankLocater[i,q]));

            if blankLocater[i,q] != “” q += 1; // this “IF” is dubious but seems critical perhaps

            blankLocater[i,q] = string_delete(storage, 1, d_at-1);

            storage3 = string_delete(storage, 1, d_at + string_length(“=========”));

            d_at2 = string_pos(“+++++++++”, blankLocater[i,q]);

            if d_at2 != 0

                {

                storage2 = blankLocater[i,q];

                blankLocater[i,q] = string_delete(blankLocater[i,q], d_at2 + string_length(“+++++++++”), string_length(blankLocater[i,q]));

                q += 1;

                storage2 = string_delete(storage2, 1, d_at2-1 + string_length(“+++++++++”));

                if storage2 != “”

                    {

                    blankLocater[i,q] = storage2;

                    }

                }

            else

                {

                //q += 1;

                break

                }

            //d_at = string_pos(“=========”, blankLocater[i,q]);

            d_at = string_pos(“=========”, blankLocater[i,q]); //used to check storage3 which failed. but wouldn’t this way create an infinite loop…?

            //d_at2 = 0; //useless line?

            stringsInLine[i] = q;

            

            }

        }   */

        

        

    //THIS TRY IS FOR THE MARBLES.

    //The above one used to get stuck in a while loop when the last blank had === but not +++. janky fix above wasn’t quite right. so we start fresh here.

    var q = 0;

    var storage = “”;

    var storage2 = “”;

    var storageForCounting = “”;

    var d_atEquals = 0;

    var d_atPluses = 0;

    var d_atEqualsCount = 0;

    var d_atPlusesCount = 0;

    for (i = 1; i < breakLineMax; i++)

        {

        q = 1;

        stringsInLine[i] = q;

        blankLocater[i,q] = txtULBlanks[i];

        

        

        d_atEquals = string_pos(“=========”, blankLocater[i,q]);

        d_atPluses = string_pos(“+++++++++”, blankLocater[i,q]);

        

        

        storageForCounting = blankLocater[i,q];

        d_atEqualsCount = 0;

        d_atPlusesCount = 0;

        while d_atEquals != 0

            {

            d_atEqualsCount += 1;

            storageForCounting = string_replace(storageForCounting,”=========”,””);

            d_atEquals = string_pos(“=========”, storageForCounting);

            }

        while d_atPluses != 0

            {

            d_atPlusesCount += 1;

            storageForCounting = string_replace(storageForCounting,”+++++++++”,””)

            d_atPluses = string_pos(“+++++++++”,storageForCounting);

            }

        

        d_atEquals = string_pos(“=========”, blankLocater[i,q]);

        d_atPluses = string_pos(“+++++++++”, blankLocater[i,q]);

        

        if d_atPlusesCount > d_atEqualsCount

            {

            while(d_atPluses > 0)

                {

                storage = blankLocater[i,q];

                blankLocater[i,q] = string_delete(blankLocater[i,q], d_atPluses + string_length(“+++++++++”), string_length(blankLocater[i,q]));

                if blankLocater[i,q] != “” q += 1; // this “IF” is dubious but seems critical perhaps

                blankLocater[i,q] = string_delete(storage, 1, d_atPluses-1 + string_length(“+++++++++”));

                //storage3 = string_delete(storage, 1, d_atPluses + string_length(“=========”));

                d_atEquals = string_pos(“=========”, blankLocater[i,q]);

                if d_atEquals != 0

                    {

                    storage2 = blankLocater[i,q];

                    blankLocater[i,q] = string_delete(blankLocater[i,q], d_atEquals, string_length(blankLocater[i,q]));

                    q += 1;

                    storage2 = string_delete(storage2, 1, d_atEquals-1);

                    if storage2 != “”

                        {

                        blankLocater[i,q] = storage2;

                        }

                    }

                d_atPluses = string_pos(“+++++++++”, blankLocater[i,q]); //used to check storage3 which failed. but wouldn’t this way create an infinite loop…?

                d_atEquals = string_pos(“=========”, blankLocater[i,q]); //useless line?

                stringsInLine[i] = q;

                }

            

            }

        else if d_atEqualsCount >= d_atPlusesCount

            {

            while(d_atEquals > 0)

                {

                storage = blankLocater[i,q];

                blankLocater[i,q] = string_delete(blankLocater[i,q], d_atEquals, string_length(blankLocater[i,q]));

                if blankLocater[i,q] != “” q += 1; // this “IF” is dubious but seems critical perhaps

                blankLocater[i,q] = string_delete(storage, 1, d_atEquals-1);

                //storage3 = string_delete(storage, 1, d_at + string_length(“=========”));

                d_atPluses = string_pos(“+++++++++”, blankLocater[i,q]);

                if d_atPluses != 0

                    {

                    storage2 = blankLocater[i,q];

                    blankLocater[i,q] = string_delete(blankLocater[i,q], d_atPluses + string_length(“+++++++++”), string_length(blankLocater[i,q]));

                    q += 1;

                    storage2 = string_delete(storage2, 1, d_atPluses-1 + string_length(“+++++++++”));

                    if storage2 != “”

                        {

                        blankLocater[i,q] = storage2;

                        }

                    }

                else

                    {

                    d_atEquals = string_pos(“=========”, blankLocater[i,q]);

                    d_atPluses = string_pos(“+++++++++”, blankLocater[i,q]);

                    stringsInLine[i] = q;

                    if string_char_at(blankLocater[i,q],string_length(blankLocater[i,q])) == ” “

                        {

                        blankLocater[i,q] = string_delete(blankLocater[i,q],string_length(blankLocater[i,q]),1);

                        }

                    break

                    }

                d_atEquals = string_pos(“=========”, blankLocater[i,q]); //used to check storage3 which failed. but wouldn’t this way create an infinite loop…?

                d_atPluses = string_pos(“+++++++++”, blankLocater[i,q]);

                stringsInLine[i] = q;

                }

            }

            

        /*

        else if d_atEquals > 0 && d_atPluses == 0 // there are only equals

            {

            while(d_atEquals > 0)

                {

                storage = blankLocater[i,q];

                blankLocater[i,q] = string_delete(blankLocater[i,q], d_atEquals, string_length(blankLocater[i,q]));

                if blankLocater[i,q] != “” q += 1; // this “IF” is dubious but seems critical perhaps

                blankLocater[i,q] = string_delete(storage, 1, d_atEquals-1);

                //storage3 = string_delete(storage, 1, d_atEquals + string_length(“=========”));

                d_atPluses = string_pos(“+++++++++”, blankLocater[i,q]);

                if d_atPluses != 0

                    {

                    storage2 = blankLocater[i,q];

                    blankLocater[i,q] = string_delete(blankLocater[i,q], d_atPluses + string_length(“+++++++++”), string_length(blankLocater[i,q]));

                    q += 1;

                    storage2 = string_delete(storage2, 1, d_atPluses-1 + string_length(“+++++++++”));

                    if storage2 != “”

                        {

                        blankLocater[i,q] = storage2;

                        }

                    }

                d_atEquals = string_pos(“=========”, blankLocater[i,q]); //used to check storage3 which failed. but wouldn’t this way create an infinite loop…?

                d_atPluses = string_pos(“+++++++++”, blankLocater[i,q]);

                stringsInLine[i] = q;

                }

            }

        

        */

        

        /* don’t even need to code this situation, I think…

        else if d_atEquals == 0 && d_atPluses == 0

            {

            

            }

        */

        }

    

    }


    //

    

    //(GOD new)

    /*

    for (i = 1; i <= breakLineMax; i+= 1)

        {

        txtTestList[i] = “”;

        list1 = string_split(txtULBlanks[3], “=========”, true);

        //for (list1) list2 = string_split(list1

        var listSize = ds_list_size(list1);

        var index;

        for (index = 0; index < listSize; index++)

            {txtTestList[i] += list1[| index]}

        }

    }

This crazy code is used in a WIP creative word game.
Its purpose is to receive a set of variables that describe this played instance of the word game (i.e., its title and its content) and then to determine where to display all of this information.

In the case of titles, it is designed to receive any possible set of text and fit it into a region at the top of the screen, squishing it and/or line-breaking it in the most elegant way possible to fit the entire string of text, and finally opting for an ellipsis if there is too much text to display in the given region. Line breaks are determined by looking for whichever single space in the text creates a top and bottom line most similar in length; the code will not break the text into more than two lines.

In the case of the body text, much is going on. The entirety of the text is contained in a one-dimensional array, in which there are two types of elements: (1) a line of text of any length — these function as the core of the word game’s story; and (2) a line of text containing symbols denoting that it is a “blank”, i.e., that it was filled in by the player(s) at a previous stage in the game — these “blanks” are what make the word game interactive and playable.

The code uses a loop to investigate each element in the array, one at a time, and it checks for which ones include a specific set of symbols (in this case, “=========” and “+++++++++”, though that will be altered to use VERY unlikely combinations of UTF-8 characters in the final version of the project). It takes such lines and deletes these signifying symbols, then checks where to break the line so that it fits on the screen (breaking it at a space when possible). It creates two new arrays when doing this: One without the symbols, which would therefore be the “readable” version of the story, and another with the symbols, allowing the program to process those lines as interactive clickable text when drawing them to the screen. (That process is another interesting one and would require another blog post to discuss).

All-in-all, I would like to discuss this code in more detail and break it down piece-by-piece. Perhaps a project for another day.