Drawing individual characters to compose a word

I am trying to take a word, divide it up into substrings with each character as one substring, then draw the,. However I cannot get even spacing between the characters. Any ideas? Here is my code:

-- Anagram
displayMode(FULLSCREEN)
-- Use this function to perform your initial setup
function setup()
    print("Hello World!")
    ChooseNewWord = true
    Word = {}
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)

    -- This sets the line thickness
    strokeWidth(5)

    if(ChooseNewWord)then
         ChooseWord()
         ChooseNewWord = false
        
         WordChosen =readLocalData("Word")
         WordLength =string.len(WordChosen)
        
         for i=0,WordLength do
            Letter=string.sub(WordChosen,i,i)
            Word[i]=Letter
         end
    end
    
    -- Do your drawing here
    fontSize(300)
    font("AmericanTypewriter")
    
    PrevValue=0
    
    for k,v in pairs(Word) do
        LetterLengthx,LetterLengthy = textSize(Word[k])
        
        spacingbetweenwords=50

        PrevValue=spacingbetweenwords+LetterLengthx+PrevValue
        
        textAlign(CENTER)
        
        text(Word[k],PrevValue,HEIGHT/2)
    end
    
    end











ChooseWord = class()

function ChooseWord:init(x)
    -- you can accept and set parameters here
    self.x = x
    Word={
    "Tap",
    "Hit",
    "Fall"
    }
    
    Number = math.random(1,#Word)
   
    WordChosen = Word[Number]
    
    saveLocalData("Word",WordChosen)
end

function ChooseWord:draw()
    -- Codea does not automatically call this method
end

function ChooseWord:touched(touch)
    -- Codea does not automatically call this method
end



Great examples! I’ll give them a go. Thanks a lot!

Here’s a version that allows you to vary the spacing width.


supportedOrientations(LANDSCAPE_ANY)

function setup()  
    parameter.number("vary",.4,1.5,1)
    font("Georgia-BoldItalic")  
    getSizes() 
    str=readProjectTab("Blend Modes:Main") -- read an existing example project  
end

function getSizes()
    w1Size=0 
    hSize=0
    for z=33,126 do
        local w,h=textSize(string.char(z))
        w1Size=math.max(w1Size,w)
        hSize=math.max(hSize,h)
    end 
end

function draw()
    background(40,40,50)
    fill(255)
    showText()
end

function showText()
    local wSize=w1Size*vary  
    local xPos=1
    local yPos=1   
    for z=1,#str do
        xPos=xPos+1
        if string.sub(str,z,z)=="\
" then
            yPos=yPos+1
            xPos=1
        end
        text(string.sub(str,z,z),xPos*wSize,HEIGHT-yPos*hSize)
    end
end

@austinmccoy Here’s an example using some of the suggestions above. I’m reading in one of the example projects just so I have something to print. Try changing the font to anything you want to see the different results.


displayMode(FULLSCREEN)

function setup()  
    font("MarkerFelt-Thin")  
    getSizes() 
    str=readProjectTab("Blend Modes:Main") -- read an existing example project  
end

function getSizes()
    wSize=0 
    hSize=0
    for z=33,126 do
        local w,h=textSize(string.char(z))
        wSize=math.max(wSize,w)
        hSize=math.max(hSize,h)
    end      
end

function draw()
    background(40,40,50)
    fill(255)
    showText()
end

function showText()
    local xPos=1
    local yPos=1   
    for z=1,#str do
        xPos=xPos+1
        if string.sub(str,z,z)=="\
" then
            yPos=yPos+1
            xPos=1
        end
        text(string.sub(str,z,z),xPos*wSize,HEIGHT-yPos*hSize)
    end
end

If you are looking for monospacing (ie exactly the same space between all characters), you could either use a monospace font like Courier, or else you could use the textSize function to measure the width of all the characters you are likely to use, and use the largest of these as your character width.

I’m pretty sure font("Courier") will give you a font that has letters that are the same width
EDIT: I didn’t see that Ignatz beat me to it

Ah, thanks a ton for the advice! The polyspacing (if that is a word) was really killing me.

I think its called proportional spacing

Test out the different fonts in Codea, I think more than one is monospace

Okay, I will! I wasn’t sure about polyspacing … I just took a guess on the Latin roots…

If you don’t want it to have to be monospaced, just run a textSize on each character, and use that to space them properly.

I think that’s what I suggested higher up.

Either way, courier works better since textSize(“”) method can be more difficult.