Thermo Mini 打印机上的 C 文本换行 [英] Text Wrapping in C on Thermo Mini Printer

查看:36
本文介绍了Thermo Mini 打印机上的 C 文本换行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很想为我的一个项目提供帮助.我是一名平面设计专业的学生,​​几乎没有编程经验.我为热敏小型打印机创建了一个程序,该程序可以根据使用的特定主题标签识别在 Twitter 上发布的推文并自动打印它们.

I would really like some help with one of my projects. I am a graphic design student and have little to no programming experience. 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.

但是,它基于 32 个字符的行长度,并将单词分成两半,而不是将整个单词移动到另一行.我的一个朋友建议使用自动换行,但我在网上找不到任何可以帮助我的东西,而且我发现的大多数代码都适用于 c++ 或 c#.

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:

// 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("
"," ");
            myPort.write(msg+"
");
        };
    }
    catch (TwitterException te) {
        println("Couldn't connect: " + te);
    };
    println("------------------------------------------------------");
    delay(20000);
}

推荐答案

既然你有这行的长度,那就没那么难了...

Since you have the length of the line, it's not that hard...

遍历字符串,一次一个字符.如果您看到一个空格,请将该位置保存为例如last_space.如果您的迭代超过最大行长度,则转到 last_space 位置并将空格转换为换行符,将位置计数器重置为零,然后从该位置重新开始.

Iterate over the string, one character at a time. If you see a space save the position as e.g. last_space. If your iteration goes over the max line length, then go to the last_space position and convert the space to a newline, reset the position counter to zero, and start over from that position.

也许实现它是这样的:

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define LINE_LENGTH 32

char text[256] = "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] != ''; current++, counter++)
    {
        if (isspace(text[current]))  // TODO: Add other delimiters here
            last_space = current;

        if (counter >= length)
        {
            text[last_space] = '
';
            counter = 0;
        }
    }
}

int main(void)
{
    printf("Before wrap:
%s
", text);

    wrap(text, LINE_LENGTH);

    printf("
After wrap:
%s
", text);

    return 0;
}

重要说明:wrap 函数修改作为 text 参数传入的缓冲区.这意味着它无法处理文字字符串,因为它们是只读的.它的工作原理是用换行符替换空格.如果您修改函数以添加多个字符,那么更好的解决方案是(重新)分配一个足够大的新缓冲区以容纳修改后的字符串.

Important note: The wrap function modifies the buffer passed in as the text argument. That means it can not handle literal strings as those are read-only. It works by replacing a space with a newline. If you modify the function to add more than one character, then a better solution would by to (re)allocate a new buffer big enough to hold the modified string.

这篇关于Thermo Mini 打印机上的 C 文本换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆