-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cc
83 lines (65 loc) · 1.86 KB
/
main.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
* A simple program to convert text input to a PNG image
*
* Written by Davorin Učakar <davorin.ucakar@gmail.com>
*/
#include <QApplication>
#include <QTextCodec>
#include <QPixmap>
#include <QPainter>
#include <QImage>
#include <iostream>
#include <cstdlib>
#include <cstring>
const int WIDTH = 1024;
const int HEIGHT = 768;
const int FONT_SIZE = 14;
int main(int argc, char** argv)
{
QApplication app(argc, argv);
QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
QStringList args = app.arguments();
args.removeAt(0);
for (const QString& s : args) {
if (s[0] == '-' || s == "-?" || s == "-h" || s == "--help") {
std::cout << "Usage: " << app.arguments()[0].data() << std::endl
<< " Input text is read from stdin and written to `out.png'." << std::endl;
return EXIT_FAILURE;
}
}
QPixmap pixmap(WIDTH, HEIGHT);
QFont font("B&H LucidaTypewriter");
QPoint point(0, FONT_SIZE);
QPainter painter(&pixmap);
pixmap.fill(Qt::black);
font.setBold(true);
font.setPixelSize(FONT_SIZE);
painter.setPen(Qt::green);
painter.setFont(font);
char line[128];
while (fgets(line, 128, stdin) != 0) {
size_t len = strlen(line);
if (len > 0 && line[len - 1] == '\n') {
line[len - 1] = '\0';
}
painter.drawText(point, line);
point.ry() += 16;
}
QImage image = pixmap.toImage();
int maxX = 0;
int maxY = 0;
for (int x = 0; x < WIDTH; ++x) {
for (int y = 0; y < HEIGHT; ++y) {
if (image.pixel(x, y) & 0x00ffffff) {
maxX = x > maxX ? x : maxX;
maxY = y > maxY ? y : maxY;
}
}
}
QPixmap croppedPixmap(maxX + 21, maxY + 11);
QPainter croppedPainter(&croppedPixmap);
croppedPixmap.fill(Qt::black);
croppedPainter.drawPixmap(10, 0, pixmap, 0, 0, maxX + 1, maxY + 1);
croppedPixmap.save("out.png");
return EXIT_SUCCESS;
}