Skip to content

Instantly share code, notes, and snippets.

@torarnv
Created February 7, 2014 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save torarnv/8864907 to your computer and use it in GitHub Desktop.
Save torarnv/8864907 to your computer and use it in GitHub Desktop.
#-------------------------------------------------
#
# Project created by QtCreator 2013-06-19T13:24:50
#
#-------------------------------------------------
QT += core core-private gui gui-private opengl #quick
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = opengl
TEMPLATE = app
LIBS += -framework ApplicationServices
OBJECTIVE_SOURCES += main.mm
#include <QApplication>
#include <QTextDocument>
#include <QTextCursor>
#include <QDebug>
#include "main.h"
#include <QtOpenGL/QGLWidget>
#if defined(QT_QUICK_LIB)
#include <QtQuick>
#endif
#include <ApplicationServices/ApplicationServices.h>
CGContextRef qt_mac_cg_context(QImage *image)
{
if (!image)
return 0;
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
uint flags = kCGImageAlphaPremultipliedFirst;
flags |= kCGBitmapByteOrder32Host;
CGContextRef ret = CGBitmapContextCreate(image->bits(), image->width(), image->height(),
8, image->bytesPerLine(), colorspace, flags);
CGContextTranslateCTM(ret, 0, image->height());
CGContextScaleCTM(ret, 1, -1);
return ret;
}
//#define TEXT "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
#define TEXT "r"
template <class T>
class Painter : public T
{
protected:
void paintEvent(QPaintEvent *) {
QPainter painter(this);
painter.fillRect(T::rect(), Qt::white);
/*painter.setRenderHint(QPainter::TextAntialiasing, true);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setRenderHint(QPainter::HighQualityAntialiasing, true);*/
/*
QTextDocument doc;
QTextCursor cursor(&doc);
QTextCharFormat format;
form
format.setFont(QFont("Helvetica", 24));
cursor.setCharFormat(format);
// cursor.insertText(QString("Hello Quick %1!").arg(reinterpret_cast<QObject *>(this)->metaObject()->className()));
cursor.insertText(QString("uuu"));
//painter.scale(2.0,2.0); // scale is 2.0
doc.drawContents(&painter);
*/
{
QImage nativeRendering(T::rect().size(), QImage::Format_ARGB32_Premultiplied);
nativeRendering.fill(Qt::white);
CGContextRef context = qt_mac_cg_context(&nativeRendering);
CTFontRef fontRef = CTFontCreateWithName((CFStringRef)@"Helvetica", 24.0f, NULL);
CFStringRef keys[] = { kCTFontAttributeName };
CFTypeRef values[] = { fontRef };
CFDictionaryRef attributes =
CFDictionaryCreate(kCFAllocatorDefault, (const void**)&keys,
(const void**)&values, sizeof(keys) / sizeof(keys[0]),
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
CFAttributedStringRef attrString =
CFAttributedStringCreate(kCFAllocatorDefault, (CFStringRef)@TEXT, attributes);
CFRelease(attributes);
CGContextSetTextMatrix(context, CGAffineTransformMakeScale(1, -1));
CTLineRef line = CTLineCreateWithAttributedString(attrString);
/*
CGContextSetShouldAntialias(context, true);
CGContextSetShouldSmoothFonts(context, true);*/
CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0);
// CGContextFillRect(context, CGRectMake(5, 25, 400, 1));
CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
CGContextSetLineWidth(context, 1.0);
CGContextBeginPath(context);
CGContextMoveToPoint(context, 5, 25);
CGContextAddLineToPoint(context, 400, 25);
CGContextStrokePath(context);
CGContextBeginPath(context);
CGContextMoveToPoint(context, 5, 55);
CGContextAddLineToPoint(context, 400, 55);
CGContextStrokePath(context);
bool yeah = true;
CGContextSetShouldAntialias(context, yeah);
CGContextSetShouldSmoothFonts(context, yeah);
// Set text position and draw the line into the graphics context
CGContextSetTextPosition(context, 5.0, 25.0);
CTLineDraw(line, context);
CFRelease(line);
CGContextRelease(context);
painter.drawImage(0, 0, nativeRendering);
}
painter.translate(0, 30);
painter.setFont(QFont("Helvetica", 24, QFont::Normal));
/* painter.setPen(QPen(Qt::red, 1.0));
painter.drawLine(5, 25, 400, 25);
painter.setPen(Qt::black);*/
painter.drawText(5, 25, QString::fromUtf8(TEXT)); // "XX X X X X p p É"
//painter.scale(1.01, 1.01); // scale is about 2.001
// painter.translate(0, 50);
//doc.drawContents(&painter);
}
};
#if 0
class RasterWidget : public QWidget, public Test
{
Q_OBJECT
protected:
void paintEvent(QPaintEvent *) {
QPainter painter(this);
painter.fillRect(rect(), Qt::white);
//painter.scale(2.0, 2.0);
/* painter.fillRect(rect(), Qt::black);
painter.setBrush(Qt::white);
painter.setPen(Qt::white);*/
draw(painter);
}
};
class OpenGLWidget : public QGLWidget, public Test
{
Q_OBJECT
protected:
void paintEvent(QPaintEvent *) {
QPainter painter(this);
draw(painter);
}
};
#endif
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Painter<QWidget> raster;
raster.resize(400, 200);
raster.show();
/*
OpenGLWidget opengl;
opengl.resize(raster.width(), raster.height());
opengl.move(raster.x() + raster.width() + 10, raster.y());
opengl.show();*/
/*
QWindow window;
window.setGeometry(50, 50, 100, 100);
window.setScreen(QGuiApplication::screens().at(1));
qDebug() << window.screen();
window.create();
qDebug() << window.screen();
QQmlEngine engine;
QQmlComponent component(&engine);
component.setData("import QtQuick 2.0; "
"Item { Rectangle { color: 'black'; anchors.fill: parent } Text { color: 'green'; font.family: 'Helvetica'; x: 10; y: 10; text: 'uuu'; font.pointSize: 24 }"
"Text {color: 'white'; font.family: 'Helvetica'; x: 10; y: 60; text: 'uuu'; font.pointSize: 24;renderType: Text.NativeRendering }"
"}"
, QUrl::fromLocalFile(""));
QQuickItem *item = qobject_cast<QQuickItem *>(component.create());
QQuickWindow quick;
quick.setScreen(QGuiApplication::screens().at(2));
quick.setPosition(-500, 0);
item->setParentItem(quick.contentItem());
quick.resize(raster.width(), raster.height());
quick.show();
qDebug() << quick.screen();*/
return a.exec();
}
#include "main.moc"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment