why couldnt it get the accurate value of w?

why couldnt it get the accurate value of w to show str normally?

--# Main

displayMode(STANDARD)

function setup()
    local bounds = vec2(600, 300)
    logoImg = nil

    fontSize(96)

    local generate = function()
        logoImg = genLogo(bounds)
    end

    parameter.text("NAME", "Title Here", generate)

    parameter.integer("FONT", 1, 9, 1, generate)

    parameter.action("Regenerate", generate)
end

function genLogo(bounds)
    local pal = {
        palettes.winter,
    }

    local fonts = {
    ("AmericanTypewriter-Bold"),
    ("ArialRoundedMTBold"),
    ("Futura-Medium"),
    ("Futura-CondensedExtraBold"),
    ("Courier-Bold"),
    ("HelveticaNeue-Light"),
    ("Noteworthy-Bold"),
    ("SourceSansPro-Bold"),
    ("Vegur-Bold"),
    }

    local pidx = 1
    local fidx = FONT or 1

    local img = image(bounds:unpack())

    font(fonts[fidx])

    setContext(img)

    renderString(NAME, bounds, pal[pidx])

    setContext()

    return img
end

function draw()
    -- This sets a dark background color 
    background(85, 85, 108, 255)

    -- This sets the line thickness
    strokeWidth(5)

    -- Do your drawing here
    sprite(logoImg, WIDTH/2, HEIGHT/2)
end

function renderString(str, bounds, scheme)
    pushStyle()

    textAlign(CENTER)

    local w,h = textSize(str)
    local xoffset = bounds.x/2 - w/3
    local ycenter = bounds.y/2
    
    str:gsub(".", function(c)

        w = textSize(c)

        local pos = vec2(xoffset, ycenter) 

        pushMatrix()

        translate(pos:unpack())


        --translate(-pos:unpack())

        blendMode(NORMAL)

        fill(scheme[2])
        text(c, 0, 0)

        popMatrix()

        xoffset = xoffset + w
    end)
    popStyle()
end

--# Palettes
palettes = {}

palettes.winter = {
    color(241, 243, 243, 255),
    color(169, 219, 223, 255),
    color(59, 130, 169, 255),
    color(38, 80, 83, 255),
}


I think my string measurement code is pretty naive, @LoopSpace did a much better job with the Anagrams example code for laying out strings.

thanks a million, i see.

function renderString(str, bounds, scheme)
    pushStyle()

    -- textAlign(CENTER) -- does nothing so remove this line
    textMode(CORNER) -- this is the key change

    local w,h = textSize(str)
    local xoffset = bounds.x/2 - w/2 -- I assume that the original w/3 was a typo
    local ycenter = bounds.y/2

Thanks a million, i understand it.

@LoopSpace thanks for the fixes. You know the Codea API better than I do.

What a simple and naive question i ask?i feel sorry for that.

@firewolf it’s the trivial stuff that can sometimes be the worst, everyone is here to help. Don’t feel bad for asking questions!