Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Help with my text reader not working on longer strings

edited January 2017 in Questions Posts: 121
-- Vertical Reader reads long strings and can scroll up and down
--[[ To use, type in
        YourPageName = [[ yourstringhere ]]



test = [[ This is a test page! 

Testing third line!

Testing Fifth line!

End test!

]]
CurrentPage = test
function setup()
    print("Hello World!")
    font("AmericanTypewriter")
    fontSize(20)
    textWrapWidth(WIDTH/2)
    textAlign(CENTER)
    textMode(CORNER)
    width, height = textSize(CurrentPage)
    print(height)
end

function draw()
    background(40, 40, 50)
    strokeWidth(5)
    fill(162, 162, 162, 255)
    text(CurrentPage, WIDTH/4, HEIGHT-height)

end

function touched(t)
    if t.state == MOVING then
        height = height - t.deltaY
        print(height)
    end
end

Update 1: changed text(CurrentPage, WIDTH/4, -height) to text(CurrentPage, WIDTH/4, HEIGHT-height)
Also, i believe i found the problem. It appears that there is a height limit to what Codea will draw of at least 1016. how do I automatically break up long strings into smaller ones?

Also, on another note, i thought you needed --]] to close --[[, not just ]]

Comments

  • dave1707dave1707 Mod
    edited January 2017 Posts: 7,470

    See the command string.sub() to break long strings into smaller ones.

  • Posts: 808

    What's the maximal amount of characters for a string?

  • dave1707dave1707 Mod
    Posts: 7,470

    A string can be just about any length, but Codea will only show so many characters on the screen depending on the character. It will show the letter i, 10556 times, but the letter a only 4212 times using textwrapWidth of WIDTH.

  • dave1707dave1707 Mod
    edited January 2017 Posts: 7,470

    As long as the h of w,h=textSize(str) doesn't exceed the HEIGHT of the screen, then the string str will show on the screen.

  • edited January 2017 Posts: 121

    Edit: This is the next iteration, i have made enough changes that I will cal the new iteration 3 see code below... the code after this code is version 2, its not labeled that though

    -- Vertical Reader reads long strings and can scroll up and down
    --[[ To use, type in
            YourPageName = [[ yourstringhere ]]
    
    --Iteration 3
    --By Thomas Z.
    
    ChapterTest = [[ This is a test page! 
    
    Testing third line!
    
    Testing Fifth line!
    
    End test!
    
    ]]
    
    
    CurrentDoc = ChapterFour
    function setup()
        print("Hello World!")
        font("AmericanTypewriter")
        fontSize(20)
        textWrapWidth(WIDTH/2)
        textAlign(CENTER)
        textMode(CORNER)
        width, docHeight = textSize(CurrentDoc)
        print(  "ScreenHeight="..HEIGHT, 
                "DocHeight="..docHeight,
                "PageCorner="..WIDTH/4,HEIGHT-docHeight)
        CurrentPage = 1
        numberOfPages = 1
        pages = {}
        if docHeight >1000 then
            StandardPageHeight = 1000
            CurrentPageHeight = StandardPageHeight
            local numofpages = docHeight/CurrentPageHeight
            numberOfPages = numofpages - numofpages%1
            print("numberofpages="..numberOfPages)
    
            --[[
            WARNING: THIS PROGRAM IS UNABLE TO AUTOMATICALLY CALCULATE THE RESULTING HEIGHT OF A SUBSTRING 
            IF YOU CHANGE THE FONT, FONTSIZE, TEXTWRAPWIDTH, TEXTALIGN OR TEXTMODE
            YOU MAY NEED TO CHANGE THE LENGTH OF THE SUBSTRING RENDERED (see j in string.sub(s,i,j)
            ALSO NOTE: text() cannot render if the string height is over 1024 pixels...
            i and j in string.sub(s, i, j) controls length of the substring. 
            use a range that under your preferred font does not exceed 1024 when
            measured with textSize()  
            --]]
    
            for i = 1, numberOfPages,1 do
                table.insert(pages, string.sub(CurrentDoc, -500 + 500*i, 500*i))
            end
        else
            table.insert(pages, CurrentDoc)
            CurrentPageHeight = docHeight
        end
    
        print(pages[CurrentPage])
    
    end
    
    function draw()
    
        background(40, 40, 50)
        strokeWidth(2)
        fill(162, 162, 162, 255)
        stroke(127, 127, 127, 255)
    
        for i = 1, numberOfPages do
            text(pages[i], WIDTH/4, HEIGHT-CurrentPageHeight-(StandardPageHeight*(i-1)))
    
            line(WIDTH/4, HEIGHT-CurrentPageHeight-(StandardPageHeight*(i-1))-16, WIDTH*.75, 
            HEIGHT-CurrentPageHeight-(StandardPageHeight*(i-1))-16)
            line(WIDTH/4, HEIGHT-CurrentPageHeight-(StandardPageHeight*(i-1))-24, WIDTH*.75, 
            HEIGHT-CurrentPageHeight-(StandardPageHeight*(i-1))-24)    
        end
    end
    
    function touched(t)
        if t.state == MOVING then
            CurrentPageHeight = CurrentPageHeight - t.deltaY
            print(CurrentPageHeight)
            if CurrentPageHeight < -32 then 
                if CurrentPage >= 2 then
                CurrentPage = CurrentPage - 1
                CurrentPageHeight = 1000
                end
            elseif CurrentPageHeight > 1032 then 
                if CurrentPageHeight <= numberOfPages then
                CurrentPage = CurrentPage + 1
                CurrentPageHeight = 1000
                end
            end
    
        end
    end
    
    

    PREVIOUS VERSION: V 0.2
    Ok Dave, i made some changes. The code is a lot less simple than before, and still not fully functional, i think i did something wring. At the best my code is ugly. Do you have suggestions, Ideas or comments?

    -- Vertical Reader reads long strings and can scroll up and down
    --[[ To use, type in
            YourPageName = [[ yourstringhere ]]
    
    
    
    ChapterTest = [[ This is a test page! 
    
    Testing third line!
    
    Testing Fifth line!
    
    End test!
    
    ]]
    
    
    CurrentDoc = ChapterFour
    function setup()
        print("Hello World!")
        font("AmericanTypewriter")
        fontSize(20)
        textWrapWidth(WIDTH/2)
        textAlign(CENTER)
        textMode(CORNER)
        width, docHeight = textSize(CurrentDoc)
        print(  "ScreenHeight="..HEIGHT, 
                "DocHeight="..docHeight,
                "PageCorner="..WIDTH/4,HEIGHT-docHeight)
        CurrentPage = 1
        numberOfPages = 1
        pages = {}
        function CalculateSubStringLength(f,fSize,tWrapW,tAlign,tMode)
            --this function calculates how long to make the substring so that it can be rendered on the iPad through Codea
    
    
        end
        if docHeight >1000 then
            CurrentPageHeight = 1000
            local numofpages = docHeight/CurrentPageHeight
            numberOfPages = numofpages - numofpages%1
            print("numberofpages="..numberOfPages)
            for i = 1, numberOfPages,1 do
                table.insert(pages, string.sub(CurrentDoc, -500 + 500*i, 500*i))
    
    
                --table.insert(pages, string.sub(CurrentDoc, 1, 500))
            end
        else
            table.insert(pages, CurrentDoc)
            CurrentPageHeight = docHeight
        end
    
        print(pages[CurrentPage])
    
    end
    
    function draw()
    
        background(40, 40, 50)
        strokeWidth(5)
        fill(162, 162, 162, 255)
    
        text(pages[CurrentPage], WIDTH/4, HEIGHT-CurrentPageHeight)
    
    end
    
    function touched(t)
        if t.state == MOVING then
            CurrentPageHeight = CurrentPageHeight - t.deltaY
            print(CurrentPageHeight)
            if CurrentPageHeight < -360 then 
                if CurrentPage >= 2 then
                CurrentPage = CurrentPage - 1
                CurrentPageHeight = 1000
                end
            elseif CurrentPageHeight > 1360 then 
                if CurrentPageHeight <= numberOfPages then
                CurrentPage = CurrentPage + 1
                CurrentPageHeight = 1000
                end
            end
    
        end
    end
    
    
  • dave1707dave1707 Mod
    Posts: 7,470

    @xThomas What are you trying to do and what does the input look like. Are you trying to do something like the Codea editor where you have lines terminated with a new line character, and you can scroll it up/down. Or do you have one long string with no new line characters that you're trying to display on the screen. How the code gets written depends a lot on what the output is supposed to do and what the input is like.

  • Posts: 121

    @dave1707 I'm trying to make just a basic text reader that i can paste long chapters of books or forum posts into. Some websites can be a bit hard to read and may nit offer changing the style. This way i can change the font and such easily in codea and read it in codea.

    So input ends up being a chapter or webpage or forum post. Outputs rendering that text as a simple wall of text.

  • dave1707dave1707 Mod
    Posts: 7,470

    @xThomas Here's something I created to see if this might work for what you're trying to do. It still needs a little work, but I'll let you do that if this works for you. You can tap the show print area icon at the upper left corner to use the parameter slider to change the font size. Close the print area to show the full screen area again. You can also slide the text up or down.


    -- insert any text to view between the [[  ]] charactrs below. str= [[ ]] displayMode(FULLSCREEN) function setup()     screenWidth=WIDTH     dy=0     tab={}     textMode(CORNER)     font("Noteworthy-Bold")     parameter.integer("size",10,30,20)     parameter.action("resize",split)     split() end function split()      fontSize(size)     w,h1=textSize("ASDFGHJKL")     tab={}     str1=""     for z=1,#str do         s=string.sub(str,z,z)         w,h=textSize(str1..s)         if w>=screenWidth or s==RETURN then             table.insert(tab,str1)             str1=s         else             str1=str1..s         end     end end              function draw()     background(194, 224, 225, 255)     fill(0)     for a,b in pairs(tab) do         text(b,0,HEIGHT-a*h1+dy)     end end function touched(t)     if t.state==MOVING then         dy=dy+t.deltaY     end end
Sign In or Register to comment.