Mosaic: motore di rendering proprio, gusci desktop e Sailfish
Primo commit del progetto: motore Rust con parser HTML, cascata CSS, layout, raster CPU, formulari, bidi, flex, tabelle, JavaScript (Boa) con eventi e costruzione di nodi, rete HTTP/HTTPS con TLS, sotto-risorse, invio dei formulari; guscio grafico per Sailfish (QML/Silica) e per desktop (minifb, distribuito come AppImage), con 11 documenti di specifica e 91 casi di reftest.
This commit is contained in:
commit
577682ae22
173 files changed
+23685
No files matched your search
@@ -0,0 +1,395 @@
|
||||
#include "engineview.h"
|
||||
|
||||
#include "engine.h"
|
||||
|
||||
#include <QDebug>
|
||||
#include <QMetaObject>
|
||||
#include <QPointer>
|
||||
|
||||
#include <cstdint>
|
||||
#include <thread>
|
||||
|
||||
#include <QCoreApplication>
|
||||
#include <QDebug>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QElapsedTimer>
|
||||
#include <QFileInfo>
|
||||
#include <QPainter>
|
||||
#include <QStandardPaths>
|
||||
|
||||
extern "C" {
|
||||
#include "engine.h"
|
||||
}
|
||||
|
||||
EngineView::EngineView(QQuickItem *parent)
|
||||
: QQuickPaintedItem(parent)
|
||||
{
|
||||
m_vivo = std::make_shared<std::atomic<bool>>(true);
|
||||
setAntialiasing(false);
|
||||
m_fontPath = resolveFont();
|
||||
// Le immagini con src relativo si risolvono nella cartella dati dell'app.
|
||||
m_baseDir = QCoreApplication::applicationDirPath() + QStringLiteral("/../share/harbour-mosaic");
|
||||
if (!QFileInfo::exists(m_baseDir))
|
||||
m_baseDir = QStringLiteral("/usr/share/harbour-mosaic");
|
||||
if (m_fontPath.isEmpty()) {
|
||||
setError(QStringLiteral("font non trovato: /usr/share/harbour-mosaic/fonts/DejaVuSans.ttf"));
|
||||
}
|
||||
}
|
||||
|
||||
void EngineView::setHtml(const QString &html)
|
||||
{
|
||||
if (m_html == html)
|
||||
return;
|
||||
m_html = html;
|
||||
emit htmlChanged();
|
||||
rerender();
|
||||
}
|
||||
|
||||
void EngineView::setExtraCss(const QString &css)
|
||||
{
|
||||
if (m_css == css)
|
||||
return;
|
||||
m_css = css;
|
||||
emit extraCssChanged();
|
||||
rerender();
|
||||
}
|
||||
|
||||
void EngineView::setZoom(qreal zoom)
|
||||
{
|
||||
const qreal clamped = qBound<qreal>(0.75, zoom, 4.0);
|
||||
if (qFuzzyCompare(clamped, m_zoom))
|
||||
return;
|
||||
m_zoom = clamped;
|
||||
emit zoomChanged();
|
||||
rerender();
|
||||
}
|
||||
|
||||
void EngineView::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
|
||||
{
|
||||
QQuickPaintedItem::geometryChanged(newGeometry, oldGeometry);
|
||||
// Solo la larghezza cambia l'impaginazione: l'altezza la decide il documento
|
||||
// (altezza automatica), quindi reagire anche a quella chiuderebbe un ciclo
|
||||
// di binding fra height e contentHeight.
|
||||
if (!qFuzzyCompare(newGeometry.width(), oldGeometry.width()))
|
||||
rerender();
|
||||
}
|
||||
|
||||
EngineView::~EngineView()
|
||||
{
|
||||
if (m_vivo) {
|
||||
m_vivo->store(false);
|
||||
}
|
||||
}
|
||||
|
||||
void EngineView::paint(QPainter *painter)
|
||||
{
|
||||
if (m_image.isNull()) {
|
||||
painter->fillRect(boundingRect(), Qt::white);
|
||||
return;
|
||||
}
|
||||
painter->drawImage(QPointF(0, 0), m_image);
|
||||
}
|
||||
|
||||
QString EngineView::resolveFont() const
|
||||
{
|
||||
QStringList candidates;
|
||||
const QByteArray env = qgetenv("HARBOUR_MOSAIC_FONT");
|
||||
if (!env.isEmpty())
|
||||
candidates << QString::fromLocal8Bit(env);
|
||||
candidates << QStringLiteral("/usr/share/harbour-mosaic/fonts/DejaVuSans.ttf")
|
||||
<< QStringLiteral("/usr/share/fonts/truetype/DejaVuSans.ttf")
|
||||
<< QStringLiteral("/usr/share/fonts/dejavu/DejaVuSans.ttf");
|
||||
for (const QString &c : candidates) {
|
||||
if (QFileInfo::exists(c))
|
||||
return c;
|
||||
}
|
||||
return QString();
|
||||
}
|
||||
|
||||
void EngineView::rerender()
|
||||
{
|
||||
if (m_html.isEmpty())
|
||||
return;
|
||||
const int w = int(width());
|
||||
if (w <= 0)
|
||||
return;
|
||||
if (m_fontPath.isEmpty()) {
|
||||
setError(QStringLiteral("font non trovato: il motore non può impaginare testo"));
|
||||
return;
|
||||
}
|
||||
// Altezza minima: il documento può essere più alto, mai più basso.
|
||||
const int minH = qMax(1, int(qMax<qreal>(0.0, height())));
|
||||
|
||||
QElapsedTimer timer;
|
||||
timer.start();
|
||||
EngineImage *img = engine_render_form(m_html.toUtf8().constData(),
|
||||
m_css.toUtf8().constData(),
|
||||
quint32(w), quint32(minH),
|
||||
float(m_zoom),
|
||||
1 /* auto_height */,
|
||||
m_fontPath.toUtf8().constData(),
|
||||
m_baseDir.toUtf8().constData(),
|
||||
stateJson().toUtf8().constData());
|
||||
m_renderMs = int(timer.elapsed());
|
||||
emit renderMsChanged();
|
||||
|
||||
if (!img) {
|
||||
setError(QStringLiteral("engine_render ha restituito NULL"));
|
||||
return;
|
||||
}
|
||||
if (img->error) {
|
||||
setError(QString::fromUtf8(img->error));
|
||||
engine_image_free(img);
|
||||
return;
|
||||
}
|
||||
|
||||
// controlli del documento: chiave, tipo e rettangolo in pixel CSS
|
||||
if (img->controls) {
|
||||
const QJsonDocument doc = QJsonDocument::fromJson(QByteArray(img->controls));
|
||||
m_controls = doc.array().toVariantList();
|
||||
emit controlsChanged();
|
||||
} else if (!m_controls.isEmpty()) {
|
||||
m_controls.clear();
|
||||
emit controlsChanged();
|
||||
}
|
||||
if (img->pixels && img->width > 0 && img->height > 0) {
|
||||
QImage view(img->pixels, int(img->width), int(img->height), QImage::Format_RGBA8888);
|
||||
m_image = view.copy();
|
||||
setError(QString());
|
||||
emit contentChanged();
|
||||
update();
|
||||
}
|
||||
engine_image_free(img);
|
||||
}
|
||||
|
||||
void EngineView::setError(const QString &error)
|
||||
{
|
||||
if (m_error == error)
|
||||
return;
|
||||
m_error = error;
|
||||
if (!error.isEmpty())
|
||||
qWarning() << "harbour-mosaic:" << error;
|
||||
emit lastErrorChanged();
|
||||
}
|
||||
|
||||
QString EngineView::stateJson() const
|
||||
{
|
||||
QJsonObject values;
|
||||
for (auto it = m_values.constBegin(); it != m_values.constEnd(); ++it) {
|
||||
values.insert(it.key(), it.value());
|
||||
}
|
||||
QJsonObject stato;
|
||||
stato.insert(QStringLiteral("values"), values);
|
||||
if (!m_focus.isEmpty()) {
|
||||
stato.insert(QStringLiteral("focus"), m_focus);
|
||||
}
|
||||
if (m_caret >= 0) {
|
||||
stato.insert(QStringLiteral("caret"), m_caret);
|
||||
}
|
||||
if (!m_openSelect.isEmpty()) {
|
||||
stato.insert(QStringLiteral("open_select"), m_openSelect);
|
||||
}
|
||||
if (m_selectionStart >= 0 && m_selectionEnd >= 0 && m_selectionStart != m_selectionEnd) {
|
||||
stato.insert(
|
||||
QStringLiteral("selection"),
|
||||
QJsonArray{m_selectionStart, m_selectionEnd});
|
||||
}
|
||||
return QString::fromUtf8(QJsonDocument(stato).toJson(QJsonDocument::Compact));
|
||||
}
|
||||
|
||||
/// Controllo che contiene il punto indicato (pixel CSS): i rettangoli arrivano
|
||||
/// dal motore, quindi il guscio non duplica la geometria.
|
||||
QString EngineView::controlAt(qreal cssX, qreal cssY) const
|
||||
{
|
||||
for (const QVariant &v : m_controls) {
|
||||
const QVariantMap m = v.toMap();
|
||||
const qreal x = m.value(QStringLiteral("x")).toDouble();
|
||||
const qreal y = m.value(QStringLiteral("y")).toDouble();
|
||||
const qreal w = m.value(QStringLiteral("w")).toDouble();
|
||||
const qreal h = m.value(QStringLiteral("h")).toDouble();
|
||||
if (cssX >= x && cssX <= x + w && cssY >= y && cssY <= y + h) {
|
||||
return m.value(QStringLiteral("key")).toString();
|
||||
}
|
||||
}
|
||||
return QString();
|
||||
}
|
||||
|
||||
QString EngineView::kindOf(const QString &key) const
|
||||
{
|
||||
for (const QVariant &v : m_controls) {
|
||||
const QVariantMap m = v.toMap();
|
||||
if (m.value(QStringLiteral("key")).toString() == key) {
|
||||
return m.value(QStringLiteral("kind")).toString();
|
||||
}
|
||||
}
|
||||
return QString();
|
||||
}
|
||||
|
||||
QString EngineView::value(const QString &key) const
|
||||
{
|
||||
return m_values.value(key);
|
||||
}
|
||||
|
||||
void EngineView::setValue(const QString &key, const QString &value)
|
||||
{
|
||||
if (key.isEmpty() || m_values.value(key) == value) {
|
||||
return;
|
||||
}
|
||||
m_values.insert(key, value);
|
||||
rerender();
|
||||
}
|
||||
|
||||
void EngineView::setFocus(const QString &key)
|
||||
{
|
||||
qDebug() << "mosaic: setFocus" << key;
|
||||
if (m_focus == key) {
|
||||
return;
|
||||
}
|
||||
m_focus = key;
|
||||
emit focusedKeyChanged();
|
||||
rerender();
|
||||
}
|
||||
|
||||
void EngineView::toggle(const QString &key)
|
||||
{
|
||||
if (key.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
const bool acceso = m_values.value(key) == QLatin1String("on");
|
||||
m_values.insert(key, acceso ? QString() : QStringLiteral("on"));
|
||||
rerender();
|
||||
}
|
||||
|
||||
void EngineView::setCaret(int pos)
|
||||
{
|
||||
if (m_caret == pos) {
|
||||
return;
|
||||
}
|
||||
m_caret = pos;
|
||||
rerender();
|
||||
}
|
||||
|
||||
void EngineView::setSelection(int inizio, int fine)
|
||||
{
|
||||
if (m_selectionStart == inizio && m_selectionEnd == fine) {
|
||||
return;
|
||||
}
|
||||
m_selectionStart = inizio;
|
||||
m_selectionEnd = fine;
|
||||
rerender();
|
||||
}
|
||||
|
||||
void EngineView::setOpenSelect(const QString &key)
|
||||
{
|
||||
qDebug() << "mosaic: setOpenSelect" << key;
|
||||
if (m_openSelect == key) {
|
||||
return;
|
||||
}
|
||||
m_openSelect = key;
|
||||
rerender();
|
||||
}
|
||||
|
||||
void EngineView::chooseOption(const QString &key, int index)
|
||||
{
|
||||
qDebug() << "mosaic: chooseOption" << key << index;
|
||||
for (const QVariant &v : m_controls) {
|
||||
const QVariantMap m = v.toMap();
|
||||
if (m.value(QStringLiteral("key")).toString() != key) {
|
||||
continue;
|
||||
}
|
||||
const QVariantList valori = m.value(QStringLiteral("values")).toList();
|
||||
if (index >= 0 && index < valori.size()) {
|
||||
m_values.insert(key, valori.at(index).toString());
|
||||
}
|
||||
break;
|
||||
}
|
||||
m_openSelect.clear();
|
||||
rerender();
|
||||
}
|
||||
|
||||
QString EngineView::textOf(const QString &key) const
|
||||
{
|
||||
for (const QVariant &v : m_controls) {
|
||||
const QVariantMap m = v.toMap();
|
||||
if (m.value(QStringLiteral("key")).toString() == key) {
|
||||
return m.value(QStringLiteral("text")).toString();
|
||||
}
|
||||
}
|
||||
return QString();
|
||||
}
|
||||
|
||||
void EngineView::toggleSelect(const QString &key)
|
||||
{
|
||||
const QString nuovo = (m_openSelect == key) ? QString() : key;
|
||||
qDebug() << "mosaic: toggleSelect" << key << "->" << nuovo;
|
||||
setOpenSelect(nuovo);
|
||||
}
|
||||
|
||||
void EngineView::fetchDone(const QString &html, const QString &errore, int stato)
|
||||
{
|
||||
if (!errore.isEmpty()) {
|
||||
m_networkError = stato > 0
|
||||
? tr("HTTP %1: %2").arg(stato).arg(errore)
|
||||
: errore;
|
||||
m_loadState = 3;
|
||||
emit loadStateChanged();
|
||||
return;
|
||||
}
|
||||
setHtml(html);
|
||||
m_loadState = 2;
|
||||
emit loadStateChanged();
|
||||
}
|
||||
|
||||
void EngineView::loadUrl(const QString &url)
|
||||
{
|
||||
const QString indirizzo = url.trimmed();
|
||||
if (indirizzo.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
if (!indirizzo.startsWith(QLatin1String("http://"))
|
||||
&& !indirizzo.startsWith(QLatin1String("https://"))) {
|
||||
m_networkError = tr("Indirizzo non valido: servono http:// o https://");
|
||||
m_loadState = 3;
|
||||
emit loadStateChanged();
|
||||
return;
|
||||
}
|
||||
|
||||
m_url = indirizzo;
|
||||
m_networkError.clear();
|
||||
m_loadState = 1;
|
||||
emit urlChanged();
|
||||
emit loadStateChanged();
|
||||
|
||||
// la richiesta gira fuori dal thread grafico; il risultato torna via coda
|
||||
auto vivo = m_vivo;
|
||||
QPointer<EngineView> self(this);
|
||||
const std::string u = indirizzo.toStdString();
|
||||
std::thread([self, vivo, u]() {
|
||||
std::string html;
|
||||
std::string errore;
|
||||
int stato = 0;
|
||||
char *messaggio = nullptr;
|
||||
char *testo = engine_fetch(u.c_str(), &stato, &messaggio);
|
||||
if (testo) {
|
||||
html = testo;
|
||||
engine_string_free(testo);
|
||||
} else if (messaggio) {
|
||||
errore = messaggio;
|
||||
engine_string_free(messaggio);
|
||||
} else {
|
||||
errore = "richiesta fallita";
|
||||
}
|
||||
if (!vivo->load() || self.isNull()) {
|
||||
return; // il guscio non c'è più
|
||||
}
|
||||
// consegna per nome: la variante con lambda non c'è su tutte le Qt dell'SDK
|
||||
QMetaObject::invokeMethod(self.data(),
|
||||
"fetchDone",
|
||||
Qt::QueuedConnection,
|
||||
Q_ARG(QString, QString::fromStdString(html)),
|
||||
Q_ARG(QString, QString::fromStdString(errore)),
|
||||
Q_ARG(int, stato));
|
||||
}).detach();
|
||||
}
|
||||
Reference in new issue
Block a user