#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    1
    Rep Power
    0

    Text Wrapping in C on Thermo Mini Printer


    I would really like some help with one of my projects. I am a graphic design student and have little to no programming experience. With help from a tutor I have created a program for a thermo mini printer that identifies tweets made on twitter based on specific hashtags used and prints them automatically.

    However, it's based on a line length of 32 chars and will split words in half instead of moving the entire word to another line. A friend of mine suggested word wrapping but I can't find anything online to help me and most code I've found tends to be for c++ or c#.

    The code so far can be found below:

    Code:
    // Build an ArrayList to hold all of the words that
    // we get from the imported tweets
    
    ArrayList<String> words = new ArrayList();
    Twitter twitter;
    import processing.serial.*;
    
    Serial myPort;  // Create object from Serial class
    int val;        // Data received from the serial port
    
    void setup() {
        String portName = Serial.list()[0];
        myPort = new Serial(this, portName, 9600);
        //Set the size of the stage, and the background to black.
        size(550,550);
        background(0);
        smooth();
    
        //Make the twitter object and prepare the query
        twitter = new TwitterFactory(cb.build()).getInstance();
    }
    
    void draw() {
    
        Query query = new Query("#R.I.P");
        query.setRpp(1);
    
        //Try making the query request.
        try {
            QueryResult result = twitter.search(query);
            ArrayList tweets = (ArrayList) result.getTweets();
    
            for (int i = 0; i < tweets.size(); i++) {
                Tweet t = (Tweet) tweets.get(i);
                String user = t.getFromUser();
                String msg = t.getText();
                Date d = t.getCreatedAt();
                println("Tweet by " + user + " at " + d + ": " + msg);
                msg = msg.replace("\n"," ");
                myPort.write(msg+"\n");
            };
        }
        catch (TwitterException te) {
            println("Couldn't connect: " + te);
        };
        println("------------------------------------------------------");
        delay(20000);
    }
    I have been given some example code, but I have no idea where this would go or what pieces of text I would need to change! see code below:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    #define LINE_LENGTH 32
    
    char text[] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer ac risus elit, id pellentesque magna. Curabitur tempor rutrum enim, sit amet interdum turpis venenatis vel. Praesent eu urna eros. Mauris sagittis tempor felis, ac feugiat est elementum sed. Praesent et augue in nibh pharetra egestas quis et lectus. Lorem ipsum.";
    
    static void wrap(char *text, const int length)
    {
        int last_space = 0;
        int counter = 0;
    
        for (int current = 0; text[current] != '\0'; current++, counter++)
        {
            if (isspace(text[current]))
                last_space = current;
    
            if (counter >= length)
            {
                text[last_space] = '\n';
                counter = 0;
            }
        }
    }
    
    int main(void)
    {
        printf("Before wrap:\n%s\n", text);
    
        wrap(text, LINE_LENGTH);
    
        printf("\nAfter wrap:\n%s\n", text);
    
        return 0;
    }

    I would really appreciate the help!
    All the best,

    Liz
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,183
    Rep Power
    9398
    0. Start at offset=0
    1a. Start a counter=32 and count down until counter=0. Each time, look at the offset+counter'th character (that will actually be at position offset+counter-1). If that's a whitespace character then stop early.
    1b. If counter=0 then it didn't find a place to break; reset counter=32 so it will break in the middle of a word
    2. Write everything from offset to offset+counter and a newline.
    3. Set offset=offset+counter (ie, where you left off) and go to step 1

    Code:
    0. [Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer ac risus elit,...
       ^
       offset
    
    1. counter=32
       [Lorem ipsum dolor sit amet, cons]ectetur adipiscing elit. Integer ac risus elit,...
       ^                                ^
       offset                           offset+counter
    
       counter=31
       [Lorem ipsum dolor sit amet, con]sectetur adipiscing elit. Integer ac risus elit,...
       ^                               ^
       offset                          offset+counter
    
       counter=30
       [Lorem ipsum dolor sit amet, co]nsectetur adipiscing elit. Integer ac risus elit,...
       ^                              ^
       offset                         offset+counter
    
       counter=29
       [Lorem ipsum dolor sit amet, c]onsectetur adipiscing elit. Integer ac risus elit,...
       ^                             ^
       offset                        offset+counter
    
       counter=28
       [Lorem ipsum dolor sit amet, ]consectetur adipiscing elit. Integer ac risus elit,...
       ^                            ^
       offset                       offset+counter
    
    2. Write "Lorem ipsum dolor sit amet, " and a newline
    
    3. Lorem ipsum dolor sit amet, [consectetur adipiscing elit. Integer ac risus elit,...
                                   ^
                                   offset
    
    1. counter=32
       Lorem ipsum dolor sit amet, [consectetur adipiscing elit. Int]eger ac risus elit,...
                                   ^                                ^
                                   offset                           offset+counter
    
       counter=31
       Lorem ipsum dolor sit amet, [consectetur adipiscing elit. In]teger ac risus elit,...
                                   ^                               ^
                                   offset                          offset+counter
    
    
       counter=30
       Lorem ipsum dolor sit amet, [consectetur adipiscing elit. I]nteger ac risus elit,...
                                   ^                              ^
                                   offset                         offset+counter
    
    
       counter=29
       Lorem ipsum dolor sit amet, [consectetur adipiscing elit. ]Integer ac risus elit,...
                                   ^                             ^
                                   offset                        offset+counter
    
    
    2. Write "consectetur adipiscing elit. " and a newline
    
    3. Lorem ipsum dolor sit amet, consectetur adipiscing elit. [Integer ac risus elit,...
                                                                ^
                                                                offset
    
    etc.
    There is a potential gotcha: if you write exactly 32 characters the printer may go to the next line automatically. If so then you'd only write the newline (in step 2) if you were writing less than 32 characters.

IMN logo majestic logo threadwatch logo seochat tools logo