Topic
IC4NOTICE: developerWorks Community will be offline May 29-30, 2015 while we upgrade to the latest version of IBM Connections. For more information, read our upgrade FAQ.
5 replies Latest Post - ‏2012-01-13T11:03:51Z by Mathias Mamsch
phan423
phan423
31 Posts
ACCEPTED ANSWER

Pinned topic Concatenate a Long String into a Smaller Text Pox on Canvas

‏2012-01-11T23:31:13Z |
I'm writing a code where it draws a number of long strings (texts) onto a canvas (dialog box) but the string is too long and I need a way to shorten it so it can fit the page. Preferably I want to put it into a text box.

For example, lets say I have the string:

The brown fox jumped over the fence and dashed through the forrest at night

And instead of having that long line printed onto the canvas, I want the code to automatically concatenate it by spaces and do this:

The brown fox jumped
over the fence and
dashed through the
forrest at night

What would be the best way to go about doing this? Is there a method to concatenate strings based on character size and whitespace index?
Updated on 2012-01-13T11:03:51Z at 2012-01-13T11:03:51Z by Mathias Mamsch
  • rain_bow
    rain_bow
    19 Posts
    ACCEPTED ANSWER

    Re: Concatenate a Long String into a Smaller Text Pox on Canvas

    ‏2012-01-12T14:13:35Z  in response to phan423
    Unless someone supplies you with a custom function, you will have to write your own function to parse the string.
  • Mathias Mamsch
    Mathias Mamsch
    1946 Posts
    ACCEPTED ANSWER

    Re: Concatenate a Long String into a Smaller Text Pox on Canvas

    ‏2012-01-12T14:38:52Z  in response to phan423

    Some years ago I implemented a simple, editable, scrollable spreadsheet on a canvas, where I did something like that. What I did was to iterate over the string and then use the 'width(Canvas, string)' and 'height(Canvas, string)' functions to get the part of the string that will fit into the canvas width. I did something like this (cannot find the full code):

    Try resizing the window!

    DB artBox = create "Try resizing this window" 
     
    string s = "The quick brown fox jumps over the lazy brown dog!"
     
    int marginx = 5
    int linespace = 5
    void renderString (DBE c, string s, int startPos, int posx, int posy, int maxx, int maxy) {
        int posToFit = startPos
        int iLen = length s 
        int h = height(c,s)
     
        if (posy > maxy) return
     
        int w = maxx-posx
     
        while (width (c, s[startPos:posToFit +1]) < w && posToFit < iLen) posToFit++
        
        draw(c, posx, h+posy, s[startPos:posToFit ])
     
        if (posToFit< iLen-1) renderString(c,s,posToFit+1,posx,posy+h+linespace,maxx,maxy)
        
        int lastPos
    }
     
    void doDrawing(DBE art){
        // repaint callback function
        int cw = width art
        int ch = width art 
        renderString (art, s,0, 10,10, cw-20, ch-20)
    } 
     
    DBE art = canvas(artBox, 200, 300, doDrawing) 
    show artBox
    

     


    You can implement some clever 'guessing' by taking the width of some middle width character and then interpolating on about how many characters you will be able to display and then search from there. Additionally you might want to think about line spaces, separators, etc. but I think it will give you the idea.

    Hope that helps, Regards, Mathias

     

     


    Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

     

    Updated on 2014-01-07T14:32:12Z at 2014-01-07T14:32:12Z by iron-man
  • llandale
    llandale
    2943 Posts
    ACCEPTED ANSWER

    Re: Concatenate a Long String into a Smaller Text Pox on Canvas

    ‏2012-01-12T14:39:27Z  in response to phan423
    This seems to be a "word wrap" issue not "concatenate"; something that's rather tricky with variable-width fonts. I don't know how to do that and I note there are few "Canvas" nor "Text Parsing" threads on this forum; so I suspect you are better off taking a step back and re-considering your requirements and expectations, perhaps a richText() box with bolded text would suffice even if you don't get a pretty canvas background.

    -Louie
  • phan423
    phan423
    31 Posts
    ACCEPTED ANSWER

    Re: Concatenate a Long String into a Smaller Text Pox on Canvas

    ‏2012-01-13T01:33:11Z  in response to phan423
    So what I'm basically trying to do is to create my own graphical visual view of a module by using links to show a fault tree instead of just using the hierarchy feature on doors. I know doors already has a "graphics mode on or off" feature. I'm trying to creating something similar to that by creating a dialog box with a canvas.

    Is there a way to access the dxl code for the "graphics mode on or off"? or what do you guys think would be the best way to create this "tree diagram" in terms of if the string i'm painting onto the canvas is too long, how do i wrap the text

    heres a picture of what i have so far and you can see why it's a problem. the text, if too long, overlaps w/ the next. i haven't drawn the lines to connect the links yet. thanks guys!
    • Mathias Mamsch
      Mathias Mamsch
      1946 Posts
      ACCEPTED ANSWER

      Re: Concatenate a Long String into a Smaller Text Pox on Canvas

      ‏2012-01-13T11:03:51Z  in response to phan423
      Well I guess in that case the code above should fit the purpose. You would only need to build in the 'word wrap', i.e. breaking the text only at word boundaries. Regards, Mathias
      Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS