Session 3: AstraFont TrueType UTF-8, Pure C++20 JPEG DCT Decoder, CSS Grid, Interactive Forms & AstraJS v3 fetch()/JSON/localStorage
Browse files- Makefile +1 -0
- README.md +11 -8
- examples/astra_data.json +6 -0
- examples/astra_nebula.jpg +0 -0
- examples/astra_session3.html +68 -0
- src/browser/browser_app.cpp +107 -10
- src/browser/browser_app.hpp +6 -1
- src/gfx/font_engine.cpp +262 -0
- src/gfx/font_engine.hpp +102 -0
- src/gfx/image_decoder.cpp +379 -0
- src/gfx/image_decoder.hpp +2 -0
- src/gfx/rasterizer.cpp +107 -29
- src/gfx/rasterizer.hpp +8 -4
- src/js/astra_js.cpp +172 -1
- src/js/astra_js.hpp +17 -3
- src/layout/layout_engine.cpp +86 -27
- src/layout/layout_engine.hpp +7 -3
- src/main.cpp +141 -15
- src/style/css_engine.cpp +66 -1
- src/style/css_engine.hpp +9 -1
Makefile
CHANGED
|
@@ -15,6 +15,7 @@ SRCS := \
|
|
| 15 |
src/layout/layout_engine.cpp \
|
| 16 |
src/js/astra_js.cpp \
|
| 17 |
src/ext/extension_runtime.cpp \
|
|
|
|
| 18 |
src/gfx/image_decoder.cpp \
|
| 19 |
src/gfx/rasterizer.cpp \
|
| 20 |
src/gfx/astra3d.cpp \
|
|
|
|
| 15 |
src/layout/layout_engine.cpp \
|
| 16 |
src/js/astra_js.cpp \
|
| 17 |
src/ext/extension_runtime.cpp \
|
| 18 |
+
src/gfx/font_engine.cpp \
|
| 19 |
src/gfx/image_decoder.cpp \
|
| 20 |
src/gfx/rasterizer.cpp \
|
| 21 |
src/gfx/astra3d.cpp \
|
README.md
CHANGED
|
@@ -22,9 +22,12 @@ Navegador web de escritorio construido desde cero en **C++20 puro**, inspirado e
|
|
| 22 |
- **IPC Multiproceso (`src/ipc/`)**: Aislamiento real mediante `fork()` y sockets binarios `AF_UNIX` (`Main`, `Content`, `RDD Media`).
|
| 23 |
- **Privacidad (`src/privacy/`)**: `AstraShield` con Total Cookie Protection particionado por `(top_level_site, origin)`, bloqueador Trie de rastreadores, limpieza de parámetros UTM/FBCLID, modo HTTPS-Only y cabeceras endurecidas (`Sec-GPC`).
|
| 24 |
- **Red y Caché (`src/net/`)**: `AstraNet` con HTTP/1.1, verificación estricta de certificados TLS 1.3 (`dlopen libssl.so.3`), descompresión `gzip`/`deflate` (`libz.so.1`) y `HttpCache` acotado (RAM + Disco).
|
| 25 |
-
- **Motor Web (`src/dom/`, `src/style/`, `src/layout/`)**: Parser HTML5, cascada CSS3 con Flexbox, Display List binario serializable (`ASD2`) y regiones interactivas (`HitRegion`).
|
| 26 |
-
- **JavaScript y Extensiones (`src/js/`, `src/ext/`)**: `AstraJS
|
| 27 |
-
- **Gráficos, 3D, Documentos y Video (`src/gfx/`, `src/docs/`, `src/media/`)**:
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
## Compilación y uso (Linux, cero dependencias de compilación)
|
| 30 |
|
|
@@ -33,17 +36,17 @@ make -j4
|
|
| 33 |
./build/astra --test
|
| 34 |
./build/astra --stress-tabs 150 --cap 2048
|
| 35 |
./build/astra --ext examples/astra-privacy-guard --url astra://home --out home.png
|
| 36 |
-
./build/astra --url file://examples/
|
| 37 |
./build/astra --url astra://3d --wireframe --out studio_3d.png
|
| 38 |
./build/astra --url astra://home --gui # Ventana interactiva nativa X11 (Linux con DISPLAY)
|
| 39 |
```
|
| 40 |
|
| 41 |
-
## Resultados verificados (Sesión
|
| 42 |
|
| 43 |
- **Compilación**: `g++ -std=c++20 -O2 -Wall -Wextra -pthread -ldl` — **0 errores, 0 warnings**.
|
| 44 |
-
- **Suite de verificación (`--test`)**: **
|
| 45 |
- **Prueba de estrés de 150 pestañas (`--stress-tabs 150 --cap 2048`)**:
|
| 46 |
-
- 150 pestañas creadas en **
|
| 47 |
-
- **RAM total real (Linux `/proc` RSS sumando los 3 procesos)**: **
|
| 48 |
- Rehidratación de pestañas deshidratadas (Tab #1): **OK**.
|
| 49 |
|
|
|
|
| 22 |
- **IPC Multiproceso (`src/ipc/`)**: Aislamiento real mediante `fork()` y sockets binarios `AF_UNIX` (`Main`, `Content`, `RDD Media`).
|
| 23 |
- **Privacidad (`src/privacy/`)**: `AstraShield` con Total Cookie Protection particionado por `(top_level_site, origin)`, bloqueador Trie de rastreadores, limpieza de parámetros UTM/FBCLID, modo HTTPS-Only y cabeceras endurecidas (`Sec-GPC`).
|
| 24 |
- **Red y Caché (`src/net/`)**: `AstraNet` con HTTP/1.1, verificación estricta de certificados TLS 1.3 (`dlopen libssl.so.3`), descompresión `gzip`/`deflate` (`libz.so.1`) y `HttpCache` acotado (RAM + Disco).
|
| 25 |
+
- **Motor Web (`src/dom/`, `src/style/`, `src/layout/`)**: Parser HTML5, cascada CSS3 con Flexbox + **CSS Grid** (`display: grid`, `grid-template-columns`, `repeat(N, 1fr)`, `fr`/`px`, `gap`), tipografía monoespaciada, campos de formulario interactivos (`<input>`/`<textarea>` con foco y escritura por teclado), Display List binario serializable (`ASD2`) y regiones interactivas (`HitRegion`).
|
| 26 |
+
- **JavaScript y Extensiones (`src/js/`, `src/ext/`)**: `AstraJS v3` (compilador a bytecode + VM de pila + GC Mark-and-Sweep + Event Loop de microtasks/timers + eventos DOM `click`/`input`/`submit` + `fetch(url)` integrado con `AstraShield`/`HttpCache`/TLS + `JSON.parse`/`JSON.stringify` + `localStorage` + métodos de `String`) y runtime de WebExtensions Manifest V3 con Declarative Net Request (DNR).
|
| 27 |
+
- **Gráficos, Tipografía, 3D, Documentos y Video (`src/gfx/`, `src/docs/`, `src/media/`)**:
|
| 28 |
+
- `AstraFont`: tipografía vectorial TrueType UTF-8 con anti-aliasing vía `dlopen("libfreetype.so.6")` (DejaVuSans + JetBrainsMono) y caché LRU acotada a 4 MiB (con fallback bitmap 8×8).
|
| 29 |
+
- `AstraImage v2`: decodificador **JPEG Baseline Sequential DCT** en C++20 puro (YCbCr 4:4:4/4:2:2/4:2:0, IDCT 8×8, cero `libjpeg`) + decodificador nativo PNG/BMP/PPM.
|
| 30 |
+
- Compositor ARGB32 con scroll APZ y ventana X11 interactiva (ratón + teclado `XLookupString`), motor 3D propio `Astra3D` (Z-buffer, normales suaves Blinn-Phong, shaders procedurales, `.obj`), visor nativo PDF (`FlateDecode`) + Markdown, y decodificador de video real MP4 H.264/Y4M en proceso RDD.
|
| 31 |
|
| 32 |
## Compilación y uso (Linux, cero dependencias de compilación)
|
| 33 |
|
|
|
|
| 36 |
./build/astra --test
|
| 37 |
./build/astra --stress-tabs 150 --cap 2048
|
| 38 |
./build/astra --ext examples/astra-privacy-guard --url astra://home --out home.png
|
| 39 |
+
./build/astra --url file://examples/astra_session3.html --click 200,235 --type "Hola Astra v3" --enter --out session3_after.png
|
| 40 |
./build/astra --url astra://3d --wireframe --out studio_3d.png
|
| 41 |
./build/astra --url astra://home --gui # Ventana interactiva nativa X11 (Linux con DISPLAY)
|
| 42 |
```
|
| 43 |
|
| 44 |
+
## Resultados verificados (Sesión 3)
|
| 45 |
|
| 46 |
- **Compilación**: `g++ -std=c++20 -O2 -Wall -Wextra -pthread -ldl` — **0 errores, 0 warnings**.
|
| 47 |
+
- **Suite de verificación (`--test`)**: **21 / 21 pruebas aprobadas** (SlabArena `munmap`/`madvise`, JSON RFC 8259, AstraShield + Total Cookie Protection, `HttpCache` RAM+Disco con evicción LRU, decodificador nativo PNG, DisplayList binario `ASD2` + `HitRegions`, `AstraJS v2/v3` Bytecode VM + GC Mark-and-Sweep + Event Loop `Microtask -> Timer` + eventos DOM `click`/`input`/`submit` + `fetch()`/`JSON`/`localStorage`, `Astra3D v2` shaders suaves Blinn-Phong, visor nativo PDF, `AstraMedia v2` MP4 H.264 multiframe acotado a 16 MiB, IPC multiproceso `Main + Content + RDD`, verificación estricta TLS con CA del sistema + cache hit, `AstraFont` TrueType UTF-8 + caché LRU 4 MiB, `AstraImage v2` JPEG DCT puro, CSS Grid multicolumna/multifila, foco + escritura + Enter en `<input>`).
|
| 48 |
- **Prueba de estrés de 150 pestañas (`--stress-tabs 150 --cap 2048`)**:
|
| 49 |
+
- 150 pestañas creadas en **~808 ms** (**~5.4 ms/pestaña** con tipografía vectorial TrueType).
|
| 50 |
+
- **RAM total real (Linux `/proc` RSS sumando los 3 procesos)**: **~21.6 MB / 2048 MB** (`main` + `content` + `rdd-media`, ~1% del techo de 2 GB).
|
| 51 |
- Rehidratación de pestañas deshidratadas (Tab #1): **OK**.
|
| 52 |
|
examples/astra_data.json
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"engine": "Astra Browser Core v3",
|
| 3 |
+
"renderer": "AstraFont TrueType + Baseline JPEG/PNG + CSS Grid",
|
| 4 |
+
"ramCeilingMB": 2048,
|
| 5 |
+
"features": ["TrueType", "JPEG-DCT", "CSS-Grid", "Forms", "fetch-API"]
|
| 6 |
+
}
|
examples/astra_nebula.jpg
ADDED
|
examples/astra_session3.html
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!doctype html>
|
| 2 |
+
<html lang="es">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="utf-8">
|
| 5 |
+
<title>Astra v3 — Sesión 3 Showcase</title>
|
| 6 |
+
<style>
|
| 7 |
+
body { background: #0a0f1a; color: #e2e8f0; padding: 18px 24px; }
|
| 8 |
+
.banner { background: #111827; border: 1px solid #1e293b; border-radius: 12px; padding: 16px 20px; margin-bottom: 14px; }
|
| 9 |
+
.kicker { color: #38bdf8; font-size: 12px; font-weight: bold; font-family: monospace; margin-bottom: 4px; }
|
| 10 |
+
.form-bar { display: grid; grid-template-columns: 1fr 190px; gap: 12px; margin-top: 10px; }
|
| 11 |
+
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; margin-bottom: 14px; }
|
| 12 |
+
.card { background: #131c2e; border: 1px solid #24324a; border-radius: 10px; padding: 12px; }
|
| 13 |
+
.label { color: #7dd3fc; font-size: 12px; font-weight: bold; font-family: monospace; margin-bottom: 6px; }
|
| 14 |
+
.status { color: #6ee7b7; font-size: 13px; font-family: monospace; margin-top: 8px; }
|
| 15 |
+
.sub { color: #94a3b8; font-size: 12px; margin-top: 8px; }
|
| 16 |
+
button { background: #0284c7; color: #ffffff; border-radius: 6px; padding: 10px 14px; font-weight: bold; font-size: 13px; }
|
| 17 |
+
</style>
|
| 18 |
+
</head>
|
| 19 |
+
<body>
|
| 20 |
+
<div class="banner">
|
| 21 |
+
<div class="kicker">SESIÓN 3 • TIPOGRAFÍA TRUETYPE VECTORIAL • JPEG NATIVO • CSS GRID • FORMULARIOS + FETCH()</div>
|
| 22 |
+
<h1 id="title-out">Astra v3: Listo para interacción</h1>
|
| 23 |
+
<div class="form-bar">
|
| 24 |
+
<input id="query-box" type="text" placeholder="Escribe un comando o consulta aquí...">
|
| 25 |
+
<button id="run-btn">Ejecutar Consulta</button>
|
| 26 |
+
</div>
|
| 27 |
+
<div id="api-status" class="status">Esperando fetch() y eventos de teclado...</div>
|
| 28 |
+
</div>
|
| 29 |
+
|
| 30 |
+
<div class="grid3">
|
| 31 |
+
<div class="card">
|
| 32 |
+
<div class="label">DECODIFICADOR JPEG DCT (C++20 PURO)</div>
|
| 33 |
+
<img src="astra_nebula.jpg" width="368" height="210">
|
| 34 |
+
<p class="sub">JPEG YCbCr 4:2:0 decodificado desde cero sin libjpeg.</p>
|
| 35 |
+
</div>
|
| 36 |
+
<div class="card">
|
| 37 |
+
<div class="label">DECODIFICADOR PNG NATIVO (FILTROS + ZLIB)</div>
|
| 38 |
+
<img src="astra_badge.png" width="368" height="210">
|
| 39 |
+
<p class="sub">PNG RGBA 8-bit compuesto en ARGB32 con suavizado.</p>
|
| 40 |
+
</div>
|
| 41 |
+
<div class="card">
|
| 42 |
+
<div class="label">ASTRA3D v2 + SHADERS BLINN-PHONG</div>
|
| 43 |
+
<astra-3d scene="torus" style="height: 210px;"></astra-3d>
|
| 44 |
+
<p class="sub">Rasterizador 3D Z-buffer con normales suaves.</p>
|
| 45 |
+
</div>
|
| 46 |
+
</div>
|
| 47 |
+
|
| 48 |
+
<script>
|
| 49 |
+
let meta = fetch("astra_data.json").json();
|
| 50 |
+
let tracker = fetch("https://doubleclick.net/ad.js");
|
| 51 |
+
localStorage.setItem("session", "3");
|
| 52 |
+
document.querySelector("#api-status").textContent =
|
| 53 |
+
"fetch(JSON): " + meta.engine + " | Shield blocked tracker: " + tracker.blockedByShield + " | LS=" + localStorage.getItem("session");
|
| 54 |
+
|
| 55 |
+
let box = document.querySelector("#query-box");
|
| 56 |
+
box.addEventListener("input", function() {
|
| 57 |
+
document.querySelector("#title-out").textContent = "Escribiendo: " + box.value;
|
| 58 |
+
});
|
| 59 |
+
box.addEventListener("submit", function() {
|
| 60 |
+
document.querySelector("#title-out").textContent = "Enviado (Enter): " + box.value.toUpperCase();
|
| 61 |
+
document.querySelector("#api-status").textContent = "Formulario procesado por AstraJS v3 • " + JSON.stringify({ query: box.value, ok: true });
|
| 62 |
+
});
|
| 63 |
+
document.querySelector("#run-btn").addEventListener("click", function() {
|
| 64 |
+
document.querySelector("#title-out").textContent = "Ejecutado (Click): " + box.value.toUpperCase();
|
| 65 |
+
});
|
| 66 |
+
</script>
|
| 67 |
+
</body>
|
| 68 |
+
</html>
|
src/browser/browser_app.cpp
CHANGED
|
@@ -29,23 +29,23 @@ bool read_local_file(const std::string& path, std::string& out) {
|
|
| 29 |
std::string build_home_html(const AstraShield& shield, size_t ext_count,
|
| 30 |
const MemorySnapshot& mem, const CacheStats& cstats) {
|
| 31 |
std::ostringstream html;
|
| 32 |
-
html << "<!doctype html><html><head><title>Astra Browser
|
| 33 |
<< "<style>"
|
| 34 |
<< "body { background: #0b0f19; color: #e2e8f0; padding: 20px 26px; }"
|
| 35 |
<< ".hero { background: #111827; border: 1px solid #1e293b; border-radius: 12px; padding: 18px 22px; margin-bottom: 16px; }"
|
| 36 |
-
<< ".kicker { color: #38bdf8; font-size: 12px; font-weight: bold; margin-bottom: 6px; }"
|
| 37 |
-
<< ".grid { display:
|
| 38 |
<< ".card { background: #131c2e; border: 1px solid #24324a; border-radius: 10px; padding: 14px; }"
|
| 39 |
-
<< ".card-title { color: #7dd3fc; font-size:
|
| 40 |
<< ".metric { color: #f8fafc; font-size: 20px; font-weight: bold; margin-bottom: 4px; }"
|
| 41 |
<< ".muted { color: #94a3b8; font-size: 12px; }"
|
| 42 |
-
<< ".media-row { display:
|
| 43 |
<< ".panel { background: #111827; border: 1px solid #1f2937; border-radius: 10px; padding: 12px; }"
|
| 44 |
<< "</style></head><body>"
|
| 45 |
<< "<div class=\"hero\">"
|
| 46 |
-
<< "<div class=\"kicker\">ASTRA
|
| 47 |
-
<< "<h1 id=\"headline\">Astra Desktop Browser — Session
|
| 48 |
-
<< "<p>Multi-process e10s architecture (Main + Content + RDD Media) with
|
| 49 |
<< "</div>"
|
| 50 |
<< "<div class=\"grid\">"
|
| 51 |
<< "<div class=\"card\"><div class=\"card-title\">MEMORY GOVERNOR</div>"
|
|
@@ -56,7 +56,7 @@ std::string build_home_html(const AstraShield& shield, size_t ext_count,
|
|
| 56 |
<< "<div class=\"muted\">Total Cookie Partitioning + Strict TLS + GPC</div></div>"
|
| 57 |
<< "<div class=\"card\"><div class=\"card-title\">WEBEXTENSIONS MV3</div>"
|
| 58 |
<< "<div class=\"metric\">" << ext_count << " Active</div>"
|
| 59 |
-
<< "<div class=\"muted\">DeclarativeNetRequest + AstraJS
|
| 60 |
<< "</div>"
|
| 61 |
<< "<div class=\"media-row\">"
|
| 62 |
<< "<div class=\"panel\"><div class=\"card-title\">ASTRA3D v2 SMOOTH SHADER ENGINE</div>"
|
|
@@ -429,12 +429,17 @@ NavigationResult BrowserApp::navigate(uint32_t tab_id, const std::string& url) {
|
|
| 429 |
|
| 430 |
tab->live_doc = std::make_unique<HtmlDocument>(HtmlDocument::parse(*tab->live_arena, html));
|
| 431 |
tab->js_vm = std::make_unique<AstraJsEngine>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 432 |
|
| 433 |
// Load external <link rel="stylesheet"> subresources through AstraShield + HttpCache
|
| 434 |
size_t ext_css_count = 0;
|
| 435 |
std::string subresource_css = fetch_external_stylesheets(*tab->live_doc, final_url, ext_css_count);
|
| 436 |
|
| 437 |
-
// Execute page <script> blocks via AstraJS
|
| 438 |
tab->console_logs.clear();
|
| 439 |
auto scripts = tab->live_doc->collect_scripts();
|
| 440 |
for (const auto& sc : scripts) {
|
|
@@ -602,6 +607,22 @@ ClickResult BrowserApp::click_at(int x, int y) {
|
|
| 602 |
out.handled = true;
|
| 603 |
out.action = "navigate";
|
| 604 |
out.detail = next_url;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 605 |
} else if (hit->kind == HitRegionKind::Button && active->live_doc && active->js_vm) {
|
| 606 |
DomNode* btn = active->live_doc->query_selector(hit->target);
|
| 607 |
if (btn) {
|
|
@@ -627,6 +648,79 @@ ClickResult BrowserApp::click_at(int x, int y) {
|
|
| 627 |
return out;
|
| 628 |
}
|
| 629 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 630 |
bool BrowserApp::decode_video_via_rdd(const std::string& path, uint32_t frame_idx, DecodedVideoFrame& out) {
|
| 631 |
if (media_worker_.alive()) {
|
| 632 |
IpcMessage req{IpcMsgType::DecodeVideoReq, next_req_id_++, {}};
|
|
@@ -716,6 +810,9 @@ bool BrowserApp::render_active_tab(const std::string& out_png_path,
|
|
| 716 |
InteractiveCallbacks cbs;
|
| 717 |
cbs.on_click = [this](int x, int y) { click_at(x, y); };
|
| 718 |
cbs.on_scroll = [this](int dy) { scroll_active_tab(static_cast<float>(dy)); };
|
|
|
|
|
|
|
|
|
|
| 719 |
cbs.on_repaint = [this](Framebuffer& f) { paint_active_tab(f); };
|
| 720 |
fb.present_x11_interactive("Astra Browser — " + (active ? active->title : "Desktop"), cbs);
|
| 721 |
}
|
|
|
|
| 29 |
std::string build_home_html(const AstraShield& shield, size_t ext_count,
|
| 30 |
const MemorySnapshot& mem, const CacheStats& cstats) {
|
| 31 |
std::ostringstream html;
|
| 32 |
+
html << "<!doctype html><html><head><title>Astra Browser v3 — Native Core</title>"
|
| 33 |
<< "<style>"
|
| 34 |
<< "body { background: #0b0f19; color: #e2e8f0; padding: 20px 26px; }"
|
| 35 |
<< ".hero { background: #111827; border: 1px solid #1e293b; border-radius: 12px; padding: 18px 22px; margin-bottom: 16px; }"
|
| 36 |
+
<< ".kicker { color: #38bdf8; font-size: 12px; font-weight: bold; font-family: monospace; margin-bottom: 6px; }"
|
| 37 |
+
<< ".grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; margin-bottom: 16px; }"
|
| 38 |
<< ".card { background: #131c2e; border: 1px solid #24324a; border-radius: 10px; padding: 14px; }"
|
| 39 |
+
<< ".card-title { color: #7dd3fc; font-size: 12px; font-weight: bold; font-family: monospace; margin-bottom: 6px; }"
|
| 40 |
<< ".metric { color: #f8fafc; font-size: 20px; font-weight: bold; margin-bottom: 4px; }"
|
| 41 |
<< ".muted { color: #94a3b8; font-size: 12px; }"
|
| 42 |
+
<< ".media-row { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }"
|
| 43 |
<< ".panel { background: #111827; border: 1px solid #1f2937; border-radius: 10px; padding: 12px; }"
|
| 44 |
<< "</style></head><body>"
|
| 45 |
<< "<div class=\"hero\">"
|
| 46 |
+
<< "<div class=\"kicker\">ASTRA v3 NATIVE C++20 | TRUETYPE VECTOR FONTS | JPEG+PNG | CSS GRID | FETCH()</div>"
|
| 47 |
+
<< "<h1 id=\"headline\">Astra Desktop Browser — Session 3</h1>"
|
| 48 |
+
<< "<p>Multi-process e10s architecture (Main + Content + RDD Media) with anti-aliased TrueType vector typography, pure C++20 Baseline JPEG & PNG decoders, CSS Grid + interactive forms, AstraJS v3 bytecode VM (GC + fetch + JSON + localStorage), strict TLS, and smooth Blinn-Phong Astra3D.</p>"
|
| 49 |
<< "</div>"
|
| 50 |
<< "<div class=\"grid\">"
|
| 51 |
<< "<div class=\"card\"><div class=\"card-title\">MEMORY GOVERNOR</div>"
|
|
|
|
| 56 |
<< "<div class=\"muted\">Total Cookie Partitioning + Strict TLS + GPC</div></div>"
|
| 57 |
<< "<div class=\"card\"><div class=\"card-title\">WEBEXTENSIONS MV3</div>"
|
| 58 |
<< "<div class=\"metric\">" << ext_count << " Active</div>"
|
| 59 |
+
<< "<div class=\"muted\">DeclarativeNetRequest + AstraJS v3 Bytecode VM</div></div>"
|
| 60 |
<< "</div>"
|
| 61 |
<< "<div class=\"media-row\">"
|
| 62 |
<< "<div class=\"panel\"><div class=\"card-title\">ASTRA3D v2 SMOOTH SHADER ENGINE</div>"
|
|
|
|
| 429 |
|
| 430 |
tab->live_doc = std::make_unique<HtmlDocument>(HtmlDocument::parse(*tab->live_arena, html));
|
| 431 |
tab->js_vm = std::make_unique<AstraJsEngine>();
|
| 432 |
+
tab->focused_input_selector.clear();
|
| 433 |
+
std::string fetch_base = final_url;
|
| 434 |
+
tab->js_vm->bind_fetch([this, fetch_base](const std::string& req_url) {
|
| 435 |
+
return perform_js_fetch(fetch_base, req_url);
|
| 436 |
+
});
|
| 437 |
|
| 438 |
// Load external <link rel="stylesheet"> subresources through AstraShield + HttpCache
|
| 439 |
size_t ext_css_count = 0;
|
| 440 |
std::string subresource_css = fetch_external_stylesheets(*tab->live_doc, final_url, ext_css_count);
|
| 441 |
|
| 442 |
+
// Execute page <script> blocks via AstraJS v3 Bytecode VM
|
| 443 |
tab->console_logs.clear();
|
| 444 |
auto scripts = tab->live_doc->collect_scripts();
|
| 445 |
for (const auto& sc : scripts) {
|
|
|
|
| 607 |
out.handled = true;
|
| 608 |
out.action = "navigate";
|
| 609 |
out.detail = next_url;
|
| 610 |
+
} else if (hit->kind == HitRegionKind::Input && active->live_doc) {
|
| 611 |
+
for (DomNode* inp : active->live_doc->query_selector_all("input")) {
|
| 612 |
+
inp->set_attr("data-astra-focused", "0");
|
| 613 |
+
}
|
| 614 |
+
for (DomNode* ta : active->live_doc->query_selector_all("textarea")) {
|
| 615 |
+
ta->set_attr("data-astra-focused", "0");
|
| 616 |
+
}
|
| 617 |
+
DomNode* el = active->live_doc->query_selector(hit->target);
|
| 618 |
+
if (el) {
|
| 619 |
+
el->set_attr("data-astra-focused", "1");
|
| 620 |
+
active->focused_input_selector = hit->target;
|
| 621 |
+
relayout_live_tab(*active);
|
| 622 |
+
out.handled = true;
|
| 623 |
+
out.action = "focus_input";
|
| 624 |
+
out.detail = hit->target;
|
| 625 |
+
}
|
| 626 |
} else if (hit->kind == HitRegionKind::Button && active->live_doc && active->js_vm) {
|
| 627 |
DomNode* btn = active->live_doc->query_selector(hit->target);
|
| 628 |
if (btn) {
|
|
|
|
| 648 |
return out;
|
| 649 |
}
|
| 650 |
|
| 651 |
+
JsFetchResult BrowserApp::perform_js_fetch(const std::string& base_url, const std::string& url) {
|
| 652 |
+
JsFetchResult out;
|
| 653 |
+
std::string resolved = HttpClient::resolve_relative_url(base_url, url);
|
| 654 |
+
if (resolved.rfind("file://", 0) == 0) {
|
| 655 |
+
std::string path = resolved.substr(7);
|
| 656 |
+
std::string body;
|
| 657 |
+
if (read_local_file(path, body)) {
|
| 658 |
+
out.ok = true;
|
| 659 |
+
out.status = 200;
|
| 660 |
+
out.body = std::move(body);
|
| 661 |
+
} else {
|
| 662 |
+
out.status = 404;
|
| 663 |
+
out.error = "File not found: " + path;
|
| 664 |
+
}
|
| 665 |
+
return out;
|
| 666 |
+
}
|
| 667 |
+
ParsedUrl base_p = parse_url(base_url);
|
| 668 |
+
std::string top_site = base_p.valid ? base_p.registrable_domain() : "";
|
| 669 |
+
HttpResponse resp = http_.get(resolved, top_site, false);
|
| 670 |
+
out.status = resp.status_code;
|
| 671 |
+
out.body = std::move(resp.body);
|
| 672 |
+
out.error = resp.error;
|
| 673 |
+
out.from_cache = resp.from_cache;
|
| 674 |
+
out.blocked_by_shield = resp.blocked_by_privacy || resp.blocked_by_extension;
|
| 675 |
+
out.ok = (!out.blocked_by_shield && resp.status_code >= 200 && resp.status_code < 400);
|
| 676 |
+
return out;
|
| 677 |
+
}
|
| 678 |
+
|
| 679 |
+
bool BrowserApp::type_text(const std::string& text) {
|
| 680 |
+
Tab* active = find_tab(active_tab_id_);
|
| 681 |
+
if (!active || !active->live_doc || active->focused_input_selector.empty()) return false;
|
| 682 |
+
DomNode* el = active->live_doc->query_selector(active->focused_input_selector);
|
| 683 |
+
if (!el) return false;
|
| 684 |
+
std::string cur = el->get_attr("value");
|
| 685 |
+
cur += text;
|
| 686 |
+
el->set_attr("value", cur);
|
| 687 |
+
if (active->js_vm) {
|
| 688 |
+
auto rep = active->js_vm->dispatch_event(el, "input", *active->live_doc);
|
| 689 |
+
for (const auto& l : rep.logs) active->console_logs.push_back(l);
|
| 690 |
+
}
|
| 691 |
+
relayout_live_tab(*active);
|
| 692 |
+
return true;
|
| 693 |
+
}
|
| 694 |
+
|
| 695 |
+
bool BrowserApp::backspace() {
|
| 696 |
+
Tab* active = find_tab(active_tab_id_);
|
| 697 |
+
if (!active || !active->live_doc || active->focused_input_selector.empty()) return false;
|
| 698 |
+
DomNode* el = active->live_doc->query_selector(active->focused_input_selector);
|
| 699 |
+
if (!el) return false;
|
| 700 |
+
std::string cur = el->get_attr("value");
|
| 701 |
+
if (!cur.empty()) cur.pop_back();
|
| 702 |
+
el->set_attr("value", cur);
|
| 703 |
+
if (active->js_vm) {
|
| 704 |
+
auto rep = active->js_vm->dispatch_event(el, "input", *active->live_doc);
|
| 705 |
+
for (const auto& l : rep.logs) active->console_logs.push_back(l);
|
| 706 |
+
}
|
| 707 |
+
relayout_live_tab(*active);
|
| 708 |
+
return true;
|
| 709 |
+
}
|
| 710 |
+
|
| 711 |
+
bool BrowserApp::press_enter() {
|
| 712 |
+
Tab* active = find_tab(active_tab_id_);
|
| 713 |
+
if (!active || !active->live_doc || active->focused_input_selector.empty()) return false;
|
| 714 |
+
DomNode* el = active->live_doc->query_selector(active->focused_input_selector);
|
| 715 |
+
if (!el) return false;
|
| 716 |
+
if (active->js_vm) {
|
| 717 |
+
auto rep = active->js_vm->dispatch_event(el, "submit", *active->live_doc);
|
| 718 |
+
for (const auto& l : rep.logs) active->console_logs.push_back(l);
|
| 719 |
+
}
|
| 720 |
+
relayout_live_tab(*active);
|
| 721 |
+
return true;
|
| 722 |
+
}
|
| 723 |
+
|
| 724 |
bool BrowserApp::decode_video_via_rdd(const std::string& path, uint32_t frame_idx, DecodedVideoFrame& out) {
|
| 725 |
if (media_worker_.alive()) {
|
| 726 |
IpcMessage req{IpcMsgType::DecodeVideoReq, next_req_id_++, {}};
|
|
|
|
| 810 |
InteractiveCallbacks cbs;
|
| 811 |
cbs.on_click = [this](int x, int y) { click_at(x, y); };
|
| 812 |
cbs.on_scroll = [this](int dy) { scroll_active_tab(static_cast<float>(dy)); };
|
| 813 |
+
cbs.on_text = [this](const std::string& t) { type_text(t); };
|
| 814 |
+
cbs.on_backspace = [this]() { backspace(); };
|
| 815 |
+
cbs.on_enter = [this]() { press_enter(); };
|
| 816 |
cbs.on_repaint = [this](Framebuffer& f) { paint_active_tab(f); };
|
| 817 |
fb.present_x11_interactive("Astra Browser — " + (active ? active->title : "Desktop"), cbs);
|
| 818 |
}
|
src/browser/browser_app.hpp
CHANGED
|
@@ -46,6 +46,7 @@ struct Tab {
|
|
| 46 |
std::unique_ptr<HtmlDocument> live_doc;
|
| 47 |
std::unique_ptr<AstraJsEngine> js_vm;
|
| 48 |
DisplayList cached_display_list;
|
|
|
|
| 49 |
std::vector<std::string> console_logs;
|
| 50 |
};
|
| 51 |
|
|
@@ -94,9 +95,12 @@ public:
|
|
| 94 |
bool dehydrate_lru_background_tab();
|
| 95 |
size_t enforce_memory_budget();
|
| 96 |
|
| 97 |
-
// Interactive APZ scroll + hit-testing click
|
| 98 |
void scroll_active_tab(float delta_y);
|
| 99 |
ClickResult click_at(int x, int y);
|
|
|
|
|
|
|
|
|
|
| 100 |
void set_3d_camera(const Camera3D& cam) { cam3d_ = cam; }
|
| 101 |
void set_video_frame(uint32_t idx) { video_frame_idx_ = idx; }
|
| 102 |
|
|
@@ -132,6 +136,7 @@ private:
|
|
| 132 |
std::string fetch_external_stylesheets(const HtmlDocument& doc,
|
| 133 |
const std::string& base_url,
|
| 134 |
size_t& loaded_count);
|
|
|
|
| 135 |
bool render_html_via_content_ipc(const std::string& html,
|
| 136 |
const std::string& extra_css,
|
| 137 |
uint32_t vw, uint32_t vh,
|
|
|
|
| 46 |
std::unique_ptr<HtmlDocument> live_doc;
|
| 47 |
std::unique_ptr<AstraJsEngine> js_vm;
|
| 48 |
DisplayList cached_display_list;
|
| 49 |
+
std::string focused_input_selector;
|
| 50 |
std::vector<std::string> console_logs;
|
| 51 |
};
|
| 52 |
|
|
|
|
| 95 |
bool dehydrate_lru_background_tab();
|
| 96 |
size_t enforce_memory_budget();
|
| 97 |
|
| 98 |
+
// Interactive APZ scroll + hit-testing click + form keyboard input
|
| 99 |
void scroll_active_tab(float delta_y);
|
| 100 |
ClickResult click_at(int x, int y);
|
| 101 |
+
bool type_text(const std::string& text);
|
| 102 |
+
bool backspace();
|
| 103 |
+
bool press_enter();
|
| 104 |
void set_3d_camera(const Camera3D& cam) { cam3d_ = cam; }
|
| 105 |
void set_video_frame(uint32_t idx) { video_frame_idx_ = idx; }
|
| 106 |
|
|
|
|
| 136 |
std::string fetch_external_stylesheets(const HtmlDocument& doc,
|
| 137 |
const std::string& base_url,
|
| 138 |
size_t& loaded_count);
|
| 139 |
+
JsFetchResult perform_js_fetch(const std::string& base_url, const std::string& url);
|
| 140 |
bool render_html_via_content_ipc(const std::string& html,
|
| 141 |
const std::string& extra_css,
|
| 142 |
uint32_t vw, uint32_t vh,
|
src/gfx/font_engine.cpp
ADDED
|
@@ -0,0 +1,262 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include "gfx/font_engine.hpp"
|
| 2 |
+
|
| 3 |
+
#include <algorithm>
|
| 4 |
+
#include <cmath>
|
| 5 |
+
#include <cstring>
|
| 6 |
+
#include <dlfcn.h>
|
| 7 |
+
|
| 8 |
+
namespace astra {
|
| 9 |
+
|
| 10 |
+
namespace {
|
| 11 |
+
|
| 12 |
+
// Verified x86_64 FreeType 2 ABI offsets on libfreetype.so.6
|
| 13 |
+
constexpr size_t kFtFaceGlyphOffset = 152;
|
| 14 |
+
constexpr size_t kFtSlotAdvanceXOffset = 128;
|
| 15 |
+
constexpr size_t kFtSlotFormatOffset = 144;
|
| 16 |
+
constexpr size_t kFtSlotBitmapRowsOffset = 152;
|
| 17 |
+
constexpr size_t kFtSlotBitmapWidthOffset = 156;
|
| 18 |
+
constexpr size_t kFtSlotBitmapPitchOffset = 160;
|
| 19 |
+
constexpr size_t kFtSlotBitmapBufferOffset = 168;
|
| 20 |
+
constexpr size_t kFtSlotPixelModeOffset = 178;
|
| 21 |
+
constexpr size_t kFtSlotBitmapLeftOffset = 192;
|
| 22 |
+
constexpr size_t kFtSlotBitmapTopOffset = 196;
|
| 23 |
+
|
| 24 |
+
constexpr uint32_t kFtGlyphFormatBitmap = 0x62697473u; // 'bits'
|
| 25 |
+
constexpr int kFtLoadRender = 4;
|
| 26 |
+
constexpr uint8_t kFtPixelModeGray = 2;
|
| 27 |
+
|
| 28 |
+
template <typename T>
|
| 29 |
+
T read_at(const void* base, size_t byte_offset) {
|
| 30 |
+
T v{};
|
| 31 |
+
std::memcpy(&v, static_cast<const uint8_t*>(base) + byte_offset, sizeof(T));
|
| 32 |
+
return v;
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
void* try_load_face(void* lib,
|
| 36 |
+
int (*ft_new_face)(void*, const char*, long, void**),
|
| 37 |
+
const std::vector<const char*>& candidates) {
|
| 38 |
+
for (const char* path : candidates) {
|
| 39 |
+
void* face = nullptr;
|
| 40 |
+
if (ft_new_face(lib, path, 0, &face) == 0 && face != nullptr) {
|
| 41 |
+
return face;
|
| 42 |
+
}
|
| 43 |
+
}
|
| 44 |
+
return nullptr;
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
int fallback_glyph_scale(float font_size) {
|
| 48 |
+
if (font_size >= 26.0f) return 3;
|
| 49 |
+
if (font_size >= 18.0f) return 2;
|
| 50 |
+
return 1;
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
} // namespace
|
| 54 |
+
|
| 55 |
+
FontEngine& FontEngine::instance() {
|
| 56 |
+
static FontEngine eng;
|
| 57 |
+
return eng;
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
FontEngine::FontEngine() {
|
| 61 |
+
dl_handle_ = dlopen("libfreetype.so.6", RTLD_LAZY);
|
| 62 |
+
if (!dl_handle_) dl_handle_ = dlopen("libfreetype.so", RTLD_LAZY);
|
| 63 |
+
if (!dl_handle_) return;
|
| 64 |
+
|
| 65 |
+
ft_init_ = reinterpret_cast<FT_Init_FreeType_fn>(dlsym(dl_handle_, "FT_Init_FreeType"));
|
| 66 |
+
ft_done_ = reinterpret_cast<FT_Done_FreeType_fn>(dlsym(dl_handle_, "FT_Done_FreeType"));
|
| 67 |
+
ft_new_face_ = reinterpret_cast<FT_New_Face_fn>(dlsym(dl_handle_, "FT_New_Face"));
|
| 68 |
+
ft_done_face_ = reinterpret_cast<FT_Done_Face_fn>(dlsym(dl_handle_, "FT_Done_Face"));
|
| 69 |
+
ft_set_pixel_sizes_ = reinterpret_cast<FT_Set_Pixel_Sizes_fn>(dlsym(dl_handle_, "FT_Set_Pixel_Sizes"));
|
| 70 |
+
ft_load_char_ = reinterpret_cast<FT_Load_Char_fn>(dlsym(dl_handle_, "FT_Load_Char"));
|
| 71 |
+
|
| 72 |
+
if (!ft_init_ || !ft_done_ || !ft_new_face_ || !ft_done_face_ ||
|
| 73 |
+
!ft_set_pixel_sizes_ || !ft_load_char_) {
|
| 74 |
+
return;
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
if (ft_init_(&ft_lib_) != 0 || !ft_lib_) return;
|
| 78 |
+
|
| 79 |
+
face_sans_reg_ = try_load_face(ft_lib_, ft_new_face_, {
|
| 80 |
+
"/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
|
| 81 |
+
"/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf",
|
| 82 |
+
"/usr/share/fonts/truetype/noto/NotoSans-Regular.ttf"
|
| 83 |
+
});
|
| 84 |
+
face_sans_bold_ = try_load_face(ft_lib_, ft_new_face_, {
|
| 85 |
+
"/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf",
|
| 86 |
+
"/usr/share/fonts/truetype/liberation/LiberationSans-Bold.ttf",
|
| 87 |
+
"/usr/share/fonts/truetype/noto/NotoSans-Bold.ttf"
|
| 88 |
+
});
|
| 89 |
+
face_mono_reg_ = try_load_face(ft_lib_, ft_new_face_, {
|
| 90 |
+
"/usr/share/fonts/truetype/jetbrains-mono/JetBrainsMono-Regular.ttf",
|
| 91 |
+
"/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf"
|
| 92 |
+
});
|
| 93 |
+
face_mono_bold_ = try_load_face(ft_lib_, ft_new_face_, {
|
| 94 |
+
"/usr/share/fonts/truetype/jetbrains-mono/JetBrainsMono-Bold.ttf",
|
| 95 |
+
"/usr/share/fonts/truetype/dejavu/DejaVuSansMono-Bold.ttf"
|
| 96 |
+
});
|
| 97 |
+
|
| 98 |
+
if (!face_sans_reg_) return;
|
| 99 |
+
|
| 100 |
+
// Runtime ABI sanity check on 'A' at 16px to guarantee safe glyph slot layout
|
| 101 |
+
if (ft_set_pixel_sizes_(face_sans_reg_, 0, 16) == 0 &&
|
| 102 |
+
ft_load_char_(face_sans_reg_, 'A', kFtLoadRender) == 0) {
|
| 103 |
+
const void* slot = read_at<const void*>(face_sans_reg_, kFtFaceGlyphOffset);
|
| 104 |
+
if (slot) {
|
| 105 |
+
uint32_t fmt = read_at<uint32_t>(slot, kFtSlotFormatOffset);
|
| 106 |
+
uint8_t pmode = read_at<uint8_t>(slot, kFtSlotPixelModeOffset);
|
| 107 |
+
uint32_t rows = read_at<uint32_t>(slot, kFtSlotBitmapRowsOffset);
|
| 108 |
+
uint32_t width = read_at<uint32_t>(slot, kFtSlotBitmapWidthOffset);
|
| 109 |
+
if (fmt == kFtGlyphFormatBitmap && pmode == kFtPixelModeGray &&
|
| 110 |
+
rows >= 6 && rows <= 32 && width >= 4 && width <= 32) {
|
| 111 |
+
active_ = true;
|
| 112 |
+
}
|
| 113 |
+
}
|
| 114 |
+
}
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
FontEngine::~FontEngine() {
|
| 118 |
+
if (face_sans_reg_ && ft_done_face_) ft_done_face_(face_sans_reg_);
|
| 119 |
+
if (face_sans_bold_ && ft_done_face_) ft_done_face_(face_sans_bold_);
|
| 120 |
+
if (face_mono_reg_ && ft_done_face_) ft_done_face_(face_mono_reg_);
|
| 121 |
+
if (face_mono_bold_ && ft_done_face_) ft_done_face_(face_mono_bold_);
|
| 122 |
+
if (ft_lib_ && ft_done_) ft_done_(ft_lib_);
|
| 123 |
+
if (dl_handle_) dlclose(dl_handle_);
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
uint32_t FontEngine::next_utf8_codepoint(std::string_view s, size_t& index) {
|
| 127 |
+
if (index >= s.size()) return 0;
|
| 128 |
+
uint8_t c0 = static_cast<uint8_t>(s[index++]);
|
| 129 |
+
if (c0 < 0x80) return c0;
|
| 130 |
+
if ((c0 & 0xE0) == 0xC0 && index < s.size()) {
|
| 131 |
+
uint8_t c1 = static_cast<uint8_t>(s[index++]);
|
| 132 |
+
return ((c0 & 0x1Fu) << 6) | (c1 & 0x3Fu);
|
| 133 |
+
}
|
| 134 |
+
if ((c0 & 0xF0) == 0xE0 && index + 1 < s.size()) {
|
| 135 |
+
uint8_t c1 = static_cast<uint8_t>(s[index++]);
|
| 136 |
+
uint8_t c2 = static_cast<uint8_t>(s[index++]);
|
| 137 |
+
return ((c0 & 0x0Fu) << 12) | ((c1 & 0x3Fu) << 6) | (c2 & 0x3Fu);
|
| 138 |
+
}
|
| 139 |
+
if ((c0 & 0xF8) == 0xF0 && index + 2 < s.size()) {
|
| 140 |
+
uint8_t c1 = static_cast<uint8_t>(s[index++]);
|
| 141 |
+
uint8_t c2 = static_cast<uint8_t>(s[index++]);
|
| 142 |
+
uint8_t c3 = static_cast<uint8_t>(s[index++]);
|
| 143 |
+
return ((c0 & 0x07u) << 18) | ((c1 & 0x3Fu) << 12) | ((c2 & 0x3Fu) << 6) | (c3 & 0x3Fu);
|
| 144 |
+
}
|
| 145 |
+
return '?';
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
void FontEngine::evict_if_needed_locked() {
|
| 149 |
+
while (cache_bytes_ > kMaxCacheBytes && !cache_.empty()) {
|
| 150 |
+
auto oldest = cache_.begin();
|
| 151 |
+
for (auto it = cache_.begin(); it != cache_.end(); ++it) {
|
| 152 |
+
if (it->second.last_used_tick < oldest->second.last_used_tick) {
|
| 153 |
+
oldest = it;
|
| 154 |
+
}
|
| 155 |
+
}
|
| 156 |
+
cache_bytes_ -= oldest->second.alpha.size() + sizeof(CachedGlyph);
|
| 157 |
+
cache_.erase(oldest);
|
| 158 |
+
}
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
const CachedGlyph* FontEngine::get_glyph(uint32_t codepoint, int pixel_size, bool bold, bool mono) {
|
| 162 |
+
if (!active_) return nullptr;
|
| 163 |
+
int px = std::clamp(pixel_size, 8, 72);
|
| 164 |
+
uint8_t sbits = static_cast<uint8_t>((bold ? 1 : 0) | (mono ? 2 : 0));
|
| 165 |
+
GlyphKey key{codepoint, static_cast<uint16_t>(px), sbits};
|
| 166 |
+
|
| 167 |
+
std::lock_guard<std::mutex> lk(mu_);
|
| 168 |
+
auto it = cache_.find(key);
|
| 169 |
+
if (it != cache_.end()) {
|
| 170 |
+
it->second.last_used_tick = ++tick_;
|
| 171 |
+
return &it->second;
|
| 172 |
+
}
|
| 173 |
+
|
| 174 |
+
void* face = nullptr;
|
| 175 |
+
if (mono) {
|
| 176 |
+
face = bold ? (face_mono_bold_ ? face_mono_bold_ : face_mono_reg_)
|
| 177 |
+
: (face_mono_reg_ ? face_mono_reg_ : face_sans_reg_);
|
| 178 |
+
} else {
|
| 179 |
+
face = bold ? (face_sans_bold_ ? face_sans_bold_ : face_sans_reg_)
|
| 180 |
+
: face_sans_reg_;
|
| 181 |
+
}
|
| 182 |
+
if (!face) face = face_sans_reg_;
|
| 183 |
+
|
| 184 |
+
if (ft_set_pixel_sizes_(face, 0, static_cast<unsigned int>(px)) != 0) return nullptr;
|
| 185 |
+
if (ft_load_char_(face, codepoint, kFtLoadRender) != 0) return nullptr;
|
| 186 |
+
|
| 187 |
+
const void* slot = read_at<const void*>(face, kFtFaceGlyphOffset);
|
| 188 |
+
if (!slot) return nullptr;
|
| 189 |
+
|
| 190 |
+
uint32_t fmt = read_at<uint32_t>(slot, kFtSlotFormatOffset);
|
| 191 |
+
if (fmt != kFtGlyphFormatBitmap) return nullptr;
|
| 192 |
+
|
| 193 |
+
uint32_t rows = read_at<uint32_t>(slot, kFtSlotBitmapRowsOffset);
|
| 194 |
+
uint32_t width = read_at<uint32_t>(slot, kFtSlotBitmapWidthOffset);
|
| 195 |
+
int32_t pitch = read_at<int32_t>(slot, kFtSlotBitmapPitchOffset);
|
| 196 |
+
const uint8_t* buf = read_at<const uint8_t*>(slot, kFtSlotBitmapBufferOffset);
|
| 197 |
+
int32_t bleft = read_at<int32_t>(slot, kFtSlotBitmapLeftOffset);
|
| 198 |
+
int32_t btop = read_at<int32_t>(slot, kFtSlotBitmapTopOffset);
|
| 199 |
+
int64_t adv_x_26_6 = read_at<int64_t>(slot, kFtSlotAdvanceXOffset);
|
| 200 |
+
|
| 201 |
+
CachedGlyph g;
|
| 202 |
+
g.width = static_cast<int>(width);
|
| 203 |
+
g.height = static_cast<int>(rows);
|
| 204 |
+
g.bearing_x = bleft;
|
| 205 |
+
g.bearing_y = btop;
|
| 206 |
+
g.advance_x = std::max(1, static_cast<int>((adv_x_26_6 + 32) >> 6));
|
| 207 |
+
g.last_used_tick = ++tick_;
|
| 208 |
+
|
| 209 |
+
if (buf && width > 0 && rows > 0 && width <= 256 && rows <= 256) {
|
| 210 |
+
g.alpha.resize(static_cast<size_t>(width) * rows, 0);
|
| 211 |
+
int abs_pitch = std::abs(pitch);
|
| 212 |
+
for (uint32_t r = 0; r < rows; ++r) {
|
| 213 |
+
const uint8_t* src_row = (pitch >= 0)
|
| 214 |
+
? (buf + static_cast<size_t>(r) * abs_pitch)
|
| 215 |
+
: (buf + static_cast<size_t>(rows - 1 - r) * abs_pitch);
|
| 216 |
+
std::memcpy(g.alpha.data() + static_cast<size_t>(r) * width, src_row, width);
|
| 217 |
+
}
|
| 218 |
+
}
|
| 219 |
+
|
| 220 |
+
cache_bytes_ += g.alpha.size() + sizeof(CachedGlyph);
|
| 221 |
+
auto [ins_it, ok] = cache_.emplace(key, std::move(g));
|
| 222 |
+
evict_if_needed_locked();
|
| 223 |
+
return &ins_it->second;
|
| 224 |
+
}
|
| 225 |
+
|
| 226 |
+
float FontEngine::measure_text(std::string_view utf8, float font_size, bool bold, bool mono) {
|
| 227 |
+
if (!active_) {
|
| 228 |
+
int scale = fallback_glyph_scale(font_size);
|
| 229 |
+
float adv = static_cast<float>(scale * 8 + (bold ? 1 : 0));
|
| 230 |
+
return static_cast<float>(utf8.size()) * adv;
|
| 231 |
+
}
|
| 232 |
+
int px = static_cast<int>(std::round(std::clamp(font_size, 8.0f, 72.0f)));
|
| 233 |
+
float total = 0.0f;
|
| 234 |
+
size_t idx = 0;
|
| 235 |
+
while (idx < utf8.size()) {
|
| 236 |
+
uint32_t cp = next_utf8_codepoint(utf8, idx);
|
| 237 |
+
const CachedGlyph* g = get_glyph(cp, px, bold, mono);
|
| 238 |
+
if (g) total += static_cast<float>(g->advance_x);
|
| 239 |
+
else total += static_cast<float>(px) * 0.5f;
|
| 240 |
+
}
|
| 241 |
+
return total;
|
| 242 |
+
}
|
| 243 |
+
|
| 244 |
+
float FontEngine::line_height(float font_size) const {
|
| 245 |
+
if (!active_) {
|
| 246 |
+
int scale = fallback_glyph_scale(font_size);
|
| 247 |
+
return static_cast<float>(scale * 8) * 1.65f;
|
| 248 |
+
}
|
| 249 |
+
return std::ceil(std::clamp(font_size, 8.0f, 72.0f) * 1.48f);
|
| 250 |
+
}
|
| 251 |
+
|
| 252 |
+
size_t FontEngine::cache_bytes() const {
|
| 253 |
+
std::lock_guard<std::mutex> lk(mu_);
|
| 254 |
+
return cache_bytes_;
|
| 255 |
+
}
|
| 256 |
+
|
| 257 |
+
size_t FontEngine::cached_glyph_count() const {
|
| 258 |
+
std::lock_guard<std::mutex> lk(mu_);
|
| 259 |
+
return cache_.size();
|
| 260 |
+
}
|
| 261 |
+
|
| 262 |
+
} // namespace astra
|
src/gfx/font_engine.hpp
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <cstdint>
|
| 4 |
+
#include <mutex>
|
| 5 |
+
#include <string>
|
| 6 |
+
#include <string_view>
|
| 7 |
+
#include <unordered_map>
|
| 8 |
+
#include <vector>
|
| 9 |
+
|
| 10 |
+
namespace astra {
|
| 11 |
+
|
| 12 |
+
struct CachedGlyph {
|
| 13 |
+
int width = 0;
|
| 14 |
+
int height = 0;
|
| 15 |
+
int bearing_x = 0;
|
| 16 |
+
int bearing_y = 0;
|
| 17 |
+
int advance_x = 0;
|
| 18 |
+
std::vector<uint8_t> alpha; // 8-bit anti-aliased coverage (width * height)
|
| 19 |
+
uint64_t last_used_tick = 0;
|
| 20 |
+
};
|
| 21 |
+
|
| 22 |
+
// AstraFont: Zero-build-dependency TrueType vector typography engine.
|
| 23 |
+
// Dynamically loads libfreetype.so.6 via dlopen at runtime (no -lfreetype link flag),
|
| 24 |
+
// renders anti-aliased UTF-8 glyphs from system DejaVuSans & JetBrainsMono .ttf faces,
|
| 25 |
+
// and caches rasterized bitmaps in a bounded 4 MiB LRU cache.
|
| 26 |
+
// Falls back gracefully to built-in 8x8 bitmap metrics if FreeType is unavailable.
|
| 27 |
+
class FontEngine {
|
| 28 |
+
public:
|
| 29 |
+
static constexpr size_t kMaxCacheBytes = 4ULL * 1024 * 1024;
|
| 30 |
+
|
| 31 |
+
static FontEngine& instance();
|
| 32 |
+
|
| 33 |
+
bool freetype_active() const { return active_; }
|
| 34 |
+
|
| 35 |
+
// Fetch or rasterize an anti-aliased glyph (returns nullptr if FreeType inactive).
|
| 36 |
+
const CachedGlyph* get_glyph(uint32_t codepoint, int pixel_size, bool bold, bool mono = false);
|
| 37 |
+
|
| 38 |
+
// Exact UTF-8 text width in pixels (shared by LayoutEngine & Framebuffer).
|
| 39 |
+
float measure_text(std::string_view utf8, float font_size, bool bold, bool mono = false);
|
| 40 |
+
|
| 41 |
+
// Line height in pixels for a given font size.
|
| 42 |
+
float line_height(float font_size) const;
|
| 43 |
+
|
| 44 |
+
// Decode next UTF-8 codepoint and advance index.
|
| 45 |
+
static uint32_t next_utf8_codepoint(std::string_view s, size_t& index);
|
| 46 |
+
|
| 47 |
+
size_t cache_bytes() const;
|
| 48 |
+
size_t cached_glyph_count() const;
|
| 49 |
+
|
| 50 |
+
private:
|
| 51 |
+
FontEngine();
|
| 52 |
+
~FontEngine();
|
| 53 |
+
|
| 54 |
+
struct GlyphKey {
|
| 55 |
+
uint32_t codepoint = 0;
|
| 56 |
+
uint16_t px_size = 14;
|
| 57 |
+
uint8_t style_bits = 0; // bit 0: bold, bit 1: mono
|
| 58 |
+
|
| 59 |
+
bool operator==(const GlyphKey& o) const {
|
| 60 |
+
return codepoint == o.codepoint && px_size == o.px_size && style_bits == o.style_bits;
|
| 61 |
+
}
|
| 62 |
+
};
|
| 63 |
+
|
| 64 |
+
struct GlyphKeyHash {
|
| 65 |
+
size_t operator()(const GlyphKey& k) const {
|
| 66 |
+
return (static_cast<size_t>(k.codepoint) * 2654435761ULL) ^
|
| 67 |
+
(static_cast<size_t>(k.px_size) << 16) ^
|
| 68 |
+
static_cast<size_t>(k.style_bits);
|
| 69 |
+
}
|
| 70 |
+
};
|
| 71 |
+
|
| 72 |
+
void evict_if_needed_locked();
|
| 73 |
+
|
| 74 |
+
bool active_ = false;
|
| 75 |
+
void* dl_handle_ = nullptr;
|
| 76 |
+
void* ft_lib_ = nullptr;
|
| 77 |
+
void* face_sans_reg_ = nullptr;
|
| 78 |
+
void* face_sans_bold_ = nullptr;
|
| 79 |
+
void* face_mono_reg_ = nullptr;
|
| 80 |
+
void* face_mono_bold_ = nullptr;
|
| 81 |
+
|
| 82 |
+
using FT_Init_FreeType_fn = int (*)(void**);
|
| 83 |
+
using FT_Done_FreeType_fn = int (*)(void*);
|
| 84 |
+
using FT_New_Face_fn = int (*)(void*, const char*, long, void**);
|
| 85 |
+
using FT_Done_Face_fn = int (*)(void*);
|
| 86 |
+
using FT_Set_Pixel_Sizes_fn = int (*)(void*, unsigned int, unsigned int);
|
| 87 |
+
using FT_Load_Char_fn = int (*)(void*, unsigned long, int);
|
| 88 |
+
|
| 89 |
+
FT_Init_FreeType_fn ft_init_ = nullptr;
|
| 90 |
+
FT_Done_FreeType_fn ft_done_ = nullptr;
|
| 91 |
+
FT_New_Face_fn ft_new_face_ = nullptr;
|
| 92 |
+
FT_Done_Face_fn ft_done_face_ = nullptr;
|
| 93 |
+
FT_Set_Pixel_Sizes_fn ft_set_pixel_sizes_ = nullptr;
|
| 94 |
+
FT_Load_Char_fn ft_load_char_ = nullptr;
|
| 95 |
+
|
| 96 |
+
mutable std::mutex mu_;
|
| 97 |
+
std::unordered_map<GlyphKey, CachedGlyph, GlyphKeyHash> cache_;
|
| 98 |
+
size_t cache_bytes_ = 0;
|
| 99 |
+
uint64_t tick_ = 0;
|
| 100 |
+
};
|
| 101 |
+
|
| 102 |
+
} // namespace astra
|
src/gfx/image_decoder.cpp
CHANGED
|
@@ -64,6 +64,9 @@ DecodedImage ImageDecoder::decode_bytes(const uint8_t* data, size_t len) {
|
|
| 64 |
if (std::memcmp(data, kPngSig, 8) == 0) {
|
| 65 |
return decode_png(data, len);
|
| 66 |
}
|
|
|
|
|
|
|
|
|
|
| 67 |
if (data[0] == 'B' && data[1] == 'M' && len >= 54) {
|
| 68 |
return decode_bmp(data, len);
|
| 69 |
}
|
|
@@ -273,4 +276,380 @@ DecodedImage ImageDecoder::decode_ppm(const uint8_t* data, size_t len) {
|
|
| 273 |
return out;
|
| 274 |
}
|
| 275 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 276 |
} // namespace astra
|
|
|
|
| 64 |
if (std::memcmp(data, kPngSig, 8) == 0) {
|
| 65 |
return decode_png(data, len);
|
| 66 |
}
|
| 67 |
+
if (data[0] == 0xFF && data[1] == 0xD8) {
|
| 68 |
+
return decode_jpeg(data, len);
|
| 69 |
+
}
|
| 70 |
if (data[0] == 'B' && data[1] == 'M' && len >= 54) {
|
| 71 |
return decode_bmp(data, len);
|
| 72 |
}
|
|
|
|
| 276 |
return out;
|
| 277 |
}
|
| 278 |
|
| 279 |
+
DecodedImage ImageDecoder::decode_jpeg(const uint8_t* data, size_t len) {
|
| 280 |
+
DecodedImage out;
|
| 281 |
+
out.format = "JPEG";
|
| 282 |
+
|
| 283 |
+
static constexpr int kZigZag[64] = {
|
| 284 |
+
0, 1, 8, 16, 9, 2, 3, 10,
|
| 285 |
+
17, 24, 32, 25, 18, 11, 4, 5,
|
| 286 |
+
12, 19, 26, 33, 40, 48, 41, 34,
|
| 287 |
+
27, 20, 13, 6, 7, 14, 21, 28,
|
| 288 |
+
35, 42, 49, 56, 57, 50, 43, 36,
|
| 289 |
+
29, 22, 15, 23, 30, 37, 44, 51,
|
| 290 |
+
58, 59, 52, 45, 38, 31, 39, 46,
|
| 291 |
+
53, 60, 61, 54, 47, 55, 62, 63
|
| 292 |
+
};
|
| 293 |
+
|
| 294 |
+
struct HuffTable {
|
| 295 |
+
bool valid = false;
|
| 296 |
+
int counts[16] = {};
|
| 297 |
+
std::vector<uint8_t> symbols;
|
| 298 |
+
std::vector<uint32_t> codes;
|
| 299 |
+
std::vector<uint8_t> lengths;
|
| 300 |
+
};
|
| 301 |
+
|
| 302 |
+
struct JpegComp {
|
| 303 |
+
uint8_t id = 0;
|
| 304 |
+
int h_samp = 1;
|
| 305 |
+
int v_samp = 1;
|
| 306 |
+
int qt_id = 0;
|
| 307 |
+
int dc_id = 0;
|
| 308 |
+
int ac_id = 0;
|
| 309 |
+
int dc_pred = 0;
|
| 310 |
+
};
|
| 311 |
+
|
| 312 |
+
uint16_t qt[4][64] = {};
|
| 313 |
+
bool qt_valid[4] = {};
|
| 314 |
+
HuffTable huff_dc[4];
|
| 315 |
+
HuffTable huff_ac[4];
|
| 316 |
+
JpegComp comps[4];
|
| 317 |
+
int num_comps = 0;
|
| 318 |
+
int width = 0, height = 0;
|
| 319 |
+
int max_h = 1, max_v = 1;
|
| 320 |
+
int restart_interval = 0;
|
| 321 |
+
size_t scan_offset = 0;
|
| 322 |
+
|
| 323 |
+
auto read_be16 = [](const uint8_t* p) -> uint16_t {
|
| 324 |
+
return static_cast<uint16_t>((static_cast<uint16_t>(p[0]) << 8) | p[1]);
|
| 325 |
+
};
|
| 326 |
+
|
| 327 |
+
size_t pos = 2;
|
| 328 |
+
while (pos + 2 <= len) {
|
| 329 |
+
if (data[pos] != 0xFF) { ++pos; continue; }
|
| 330 |
+
while (pos < len && data[pos] == 0xFF) ++pos;
|
| 331 |
+
if (pos >= len) break;
|
| 332 |
+
uint8_t marker = data[pos++];
|
| 333 |
+
if (marker == 0xD9) break; // EOI
|
| 334 |
+
if (marker == 0x00 || (marker >= 0xD0 && marker <= 0xD7) || marker == 0x01) continue;
|
| 335 |
+
|
| 336 |
+
if (pos + 2 > len) break;
|
| 337 |
+
uint16_t seg_len = read_be16(data + pos);
|
| 338 |
+
if (seg_len < 2 || pos + seg_len > len) break;
|
| 339 |
+
const uint8_t* seg = data + pos + 2;
|
| 340 |
+
size_t slen = seg_len - 2;
|
| 341 |
+
|
| 342 |
+
if (marker == 0xDB) { // DQT
|
| 343 |
+
size_t o = 0;
|
| 344 |
+
while (o < slen) {
|
| 345 |
+
uint8_t pq_tq = seg[o++];
|
| 346 |
+
int prec = pq_tq >> 4;
|
| 347 |
+
int tq = pq_tq & 0x0F;
|
| 348 |
+
if (tq > 3) break;
|
| 349 |
+
if (prec == 0) {
|
| 350 |
+
if (o + 64 > slen) break;
|
| 351 |
+
for (int i = 0; i < 64; ++i) qt[tq][kZigZag[i]] = seg[o++];
|
| 352 |
+
} else {
|
| 353 |
+
if (o + 128 > slen) break;
|
| 354 |
+
for (int i = 0; i < 64; ++i) {
|
| 355 |
+
qt[tq][kZigZag[i]] = read_be16(seg + o);
|
| 356 |
+
o += 2;
|
| 357 |
+
}
|
| 358 |
+
}
|
| 359 |
+
qt_valid[tq] = true;
|
| 360 |
+
}
|
| 361 |
+
} else if (marker == 0xC0) { // SOF0 Baseline DCT
|
| 362 |
+
if (slen < 6) { out.error = "Truncated SOF0"; return out; }
|
| 363 |
+
uint8_t precision = seg[0];
|
| 364 |
+
height = read_be16(seg + 1);
|
| 365 |
+
width = read_be16(seg + 3);
|
| 366 |
+
num_comps = seg[5];
|
| 367 |
+
if (precision != 8 || (num_comps != 1 && num_comps != 3) || slen < static_cast<size_t>(6 + num_comps * 3)) {
|
| 368 |
+
out.error = "Unsupported JPEG SOF0 format";
|
| 369 |
+
return out;
|
| 370 |
+
}
|
| 371 |
+
max_h = 1;
|
| 372 |
+
max_v = 1;
|
| 373 |
+
for (int c = 0; c < num_comps; ++c) {
|
| 374 |
+
comps[c].id = seg[6 + c * 3];
|
| 375 |
+
uint8_t hv = seg[6 + c * 3 + 1];
|
| 376 |
+
comps[c].h_samp = std::max(1, static_cast<int>(hv >> 4));
|
| 377 |
+
comps[c].v_samp = std::max(1, static_cast<int>(hv & 0x0F));
|
| 378 |
+
comps[c].qt_id = seg[6 + c * 3 + 2] & 3;
|
| 379 |
+
max_h = std::max(max_h, comps[c].h_samp);
|
| 380 |
+
max_v = std::max(max_v, comps[c].v_samp);
|
| 381 |
+
}
|
| 382 |
+
} else if (marker == 0xC2) {
|
| 383 |
+
out.error = "Progressive JPEG (SOF2) not yet supported";
|
| 384 |
+
return out;
|
| 385 |
+
} else if (marker == 0xC4) { // DHT
|
| 386 |
+
size_t o = 0;
|
| 387 |
+
while (o + 17 <= slen) {
|
| 388 |
+
uint8_t tc_th = seg[o++];
|
| 389 |
+
int tc = tc_th >> 4;
|
| 390 |
+
int th = tc_th & 0x0F;
|
| 391 |
+
if (th > 3) break;
|
| 392 |
+
HuffTable& ht = (tc == 0) ? huff_dc[th] : huff_ac[th];
|
| 393 |
+
ht = HuffTable{};
|
| 394 |
+
int total_syms = 0;
|
| 395 |
+
for (int i = 0; i < 16; ++i) {
|
| 396 |
+
ht.counts[i] = seg[o++];
|
| 397 |
+
total_syms += ht.counts[i];
|
| 398 |
+
}
|
| 399 |
+
if (o + static_cast<size_t>(total_syms) > slen) break;
|
| 400 |
+
ht.symbols.assign(seg + o, seg + o + total_syms);
|
| 401 |
+
o += static_cast<size_t>(total_syms);
|
| 402 |
+
uint32_t code = 0;
|
| 403 |
+
for (int bits = 1; bits <= 16; ++bits) {
|
| 404 |
+
for (int k = 0; k < ht.counts[bits - 1]; ++k) {
|
| 405 |
+
ht.codes.push_back(code++);
|
| 406 |
+
ht.lengths.push_back(static_cast<uint8_t>(bits));
|
| 407 |
+
}
|
| 408 |
+
code <<= 1;
|
| 409 |
+
}
|
| 410 |
+
ht.valid = true;
|
| 411 |
+
}
|
| 412 |
+
} else if (marker == 0xDD) { // DRI
|
| 413 |
+
if (slen >= 2) restart_interval = read_be16(seg);
|
| 414 |
+
} else if (marker == 0xDA) { // SOS
|
| 415 |
+
if (slen < 1) { out.error = "Invalid SOS"; return out; }
|
| 416 |
+
int ns = seg[0];
|
| 417 |
+
if (slen < static_cast<size_t>(1 + ns * 2 + 3)) { out.error = "Truncated SOS"; return out; }
|
| 418 |
+
for (int i = 0; i < ns; ++i) {
|
| 419 |
+
uint8_t cid = seg[1 + i * 2];
|
| 420 |
+
uint8_t td_ta = seg[1 + i * 2 + 1];
|
| 421 |
+
for (int c = 0; c < num_comps; ++c) {
|
| 422 |
+
if (comps[c].id == cid) {
|
| 423 |
+
comps[c].dc_id = (td_ta >> 4) & 3;
|
| 424 |
+
comps[c].ac_id = td_ta & 3;
|
| 425 |
+
}
|
| 426 |
+
}
|
| 427 |
+
}
|
| 428 |
+
scan_offset = pos + seg_len;
|
| 429 |
+
break;
|
| 430 |
+
}
|
| 431 |
+
pos += seg_len;
|
| 432 |
+
}
|
| 433 |
+
|
| 434 |
+
if (width <= 0 || height <= 0 || width > 8192 || height > 8192 || scan_offset == 0 || num_comps == 0) {
|
| 435 |
+
out.error = "Invalid or incomplete Baseline JPEG";
|
| 436 |
+
return out;
|
| 437 |
+
}
|
| 438 |
+
|
| 439 |
+
// Bit reader for JPEG entropy-coded scan (handles 0xFF 0x00 byte stuffing)
|
| 440 |
+
struct BitReader {
|
| 441 |
+
const uint8_t* buf;
|
| 442 |
+
size_t len;
|
| 443 |
+
size_t pos;
|
| 444 |
+
uint32_t bit_buf = 0;
|
| 445 |
+
int bit_cnt = 0;
|
| 446 |
+
|
| 447 |
+
uint8_t next_byte() {
|
| 448 |
+
if (pos >= len) return 0;
|
| 449 |
+
uint8_t b = buf[pos++];
|
| 450 |
+
if (b == 0xFF && pos < len) {
|
| 451 |
+
uint8_t next = buf[pos];
|
| 452 |
+
if (next == 0x00) {
|
| 453 |
+
++pos; // stuffed zero
|
| 454 |
+
} else if (next >= 0xD0 && next <= 0xD7) {
|
| 455 |
+
++pos; // restart marker consumed
|
| 456 |
+
return next_byte();
|
| 457 |
+
} else {
|
| 458 |
+
--pos; // hit EOI or next marker
|
| 459 |
+
return 0;
|
| 460 |
+
}
|
| 461 |
+
}
|
| 462 |
+
return b;
|
| 463 |
+
}
|
| 464 |
+
|
| 465 |
+
int read_bit() {
|
| 466 |
+
if (bit_cnt == 0) {
|
| 467 |
+
bit_buf = next_byte();
|
| 468 |
+
bit_cnt = 8;
|
| 469 |
+
}
|
| 470 |
+
--bit_cnt;
|
| 471 |
+
return static_cast<int>((bit_buf >> bit_cnt) & 1u);
|
| 472 |
+
}
|
| 473 |
+
|
| 474 |
+
int read_bits(int n) {
|
| 475 |
+
int v = 0;
|
| 476 |
+
for (int i = 0; i < n; ++i) v = (v << 1) | read_bit();
|
| 477 |
+
return v;
|
| 478 |
+
}
|
| 479 |
+
|
| 480 |
+
int receive_extend(int n) {
|
| 481 |
+
if (n == 0) return 0;
|
| 482 |
+
int v = read_bits(n);
|
| 483 |
+
int half = 1 << (n - 1);
|
| 484 |
+
if (v < half) v -= (1 << n) - 1;
|
| 485 |
+
return v;
|
| 486 |
+
}
|
| 487 |
+
|
| 488 |
+
int decode_huff(const HuffTable& ht) {
|
| 489 |
+
uint32_t code = 0;
|
| 490 |
+
size_t idx = 0;
|
| 491 |
+
for (int bits = 1; bits <= 16; ++bits) {
|
| 492 |
+
code = (code << 1) | static_cast<uint32_t>(read_bit());
|
| 493 |
+
int cnt = ht.counts[bits - 1];
|
| 494 |
+
for (int k = 0; k < cnt; ++k, ++idx) {
|
| 495 |
+
if (ht.codes[idx] == code) return ht.symbols[idx];
|
| 496 |
+
}
|
| 497 |
+
}
|
| 498 |
+
return -1;
|
| 499 |
+
}
|
| 500 |
+
|
| 501 |
+
void align_byte() {
|
| 502 |
+
bit_cnt = 0;
|
| 503 |
+
bit_buf = 0;
|
| 504 |
+
}
|
| 505 |
+
} br{data, len, scan_offset};
|
| 506 |
+
|
| 507 |
+
// Precompute 8x8 cosine table for separable 2D IDCT
|
| 508 |
+
static float cos_tbl[8][8];
|
| 509 |
+
static bool cos_init = false;
|
| 510 |
+
if (!cos_init) {
|
| 511 |
+
constexpr float kPi = 3.14159265358979323846f;
|
| 512 |
+
for (int x = 0; x < 8; ++x) {
|
| 513 |
+
for (int u = 0; u < 8; ++u) {
|
| 514 |
+
float alpha = (u == 0) ? 0.35355339f : 0.5f; // 1/(2*sqrt(2)) vs 1/2
|
| 515 |
+
cos_tbl[x][u] = alpha * std::cos((2.0f * x + 1.0f) * u * kPi / 16.0f);
|
| 516 |
+
}
|
| 517 |
+
}
|
| 518 |
+
cos_init = true;
|
| 519 |
+
}
|
| 520 |
+
|
| 521 |
+
auto idct_8x8 = [&](const int coef[64], float out_px[64]) {
|
| 522 |
+
float tmp[64];
|
| 523 |
+
for (int y = 0; y < 8; ++y) {
|
| 524 |
+
for (int x = 0; x < 8; ++x) {
|
| 525 |
+
float sum = 0.0f;
|
| 526 |
+
for (int u = 0; u < 8; ++u) {
|
| 527 |
+
sum += static_cast<float>(coef[y * 8 + u]) * cos_tbl[x][u];
|
| 528 |
+
}
|
| 529 |
+
tmp[y * 8 + x] = sum;
|
| 530 |
+
}
|
| 531 |
+
}
|
| 532 |
+
for (int x = 0; x < 8; ++x) {
|
| 533 |
+
for (int y = 0; y < 8; ++y) {
|
| 534 |
+
float sum = 0.0f;
|
| 535 |
+
for (int v = 0; v < 8; ++v) {
|
| 536 |
+
sum += tmp[v * 8 + x] * cos_tbl[y][v];
|
| 537 |
+
}
|
| 538 |
+
out_px[y * 8 + x] = sum + 128.0f;
|
| 539 |
+
}
|
| 540 |
+
}
|
| 541 |
+
};
|
| 542 |
+
|
| 543 |
+
int mcu_w = max_h * 8;
|
| 544 |
+
int mcu_h = max_v * 8;
|
| 545 |
+
int mcus_x = (width + mcu_w - 1) / mcu_w;
|
| 546 |
+
int mcus_y = (height + mcu_h - 1) / mcu_h;
|
| 547 |
+
|
| 548 |
+
out.width = width;
|
| 549 |
+
out.height = height;
|
| 550 |
+
out.argb.assign(static_cast<size_t>(width) * height, 0xFF000000u);
|
| 551 |
+
|
| 552 |
+
// Component block storage per MCU (up to 4x4 blocks per component)
|
| 553 |
+
float comp_buf[3][32 * 32];
|
| 554 |
+
int mcu_count = 0;
|
| 555 |
+
|
| 556 |
+
for (int my = 0; my < mcus_y; ++my) {
|
| 557 |
+
for (int mx = 0; mx < mcus_x; ++mx) {
|
| 558 |
+
if (restart_interval > 0 && mcu_count > 0 && (mcu_count % restart_interval) == 0) {
|
| 559 |
+
br.align_byte();
|
| 560 |
+
for (int c = 0; c < num_comps; ++c) comps[c].dc_pred = 0;
|
| 561 |
+
}
|
| 562 |
+
++mcu_count;
|
| 563 |
+
|
| 564 |
+
for (int c = 0; c < num_comps; ++c) {
|
| 565 |
+
JpegComp& cp = comps[c];
|
| 566 |
+
const HuffTable& dc_ht = huff_dc[cp.dc_id];
|
| 567 |
+
const HuffTable& ac_ht = huff_ac[cp.ac_id];
|
| 568 |
+
if (!dc_ht.valid || !ac_ht.valid || !qt_valid[cp.qt_id]) {
|
| 569 |
+
out.error = "Missing JPEG Huffman or Quantization table";
|
| 570 |
+
return out;
|
| 571 |
+
}
|
| 572 |
+
int cw = cp.h_samp * 8;
|
| 573 |
+
for (int bv = 0; bv < cp.v_samp; ++bv) {
|
| 574 |
+
for (int bh = 0; bh < cp.h_samp; ++bh) {
|
| 575 |
+
int coef[64] = {};
|
| 576 |
+
int s = br.decode_huff(dc_ht);
|
| 577 |
+
if (s < 0) s = 0;
|
| 578 |
+
int diff = br.receive_extend(s & 0x0F);
|
| 579 |
+
cp.dc_pred += diff;
|
| 580 |
+
coef[0] = cp.dc_pred * qt[cp.qt_id][0];
|
| 581 |
+
|
| 582 |
+
int k = 1;
|
| 583 |
+
while (k < 64) {
|
| 584 |
+
int rs = br.decode_huff(ac_ht);
|
| 585 |
+
if (rs <= 0) break; // EOB or error
|
| 586 |
+
int run = rs >> 4;
|
| 587 |
+
int sz = rs & 0x0F;
|
| 588 |
+
if (sz == 0) {
|
| 589 |
+
if (run == 15) { k += 16; continue; }
|
| 590 |
+
break;
|
| 591 |
+
}
|
| 592 |
+
k += run;
|
| 593 |
+
if (k >= 64) break;
|
| 594 |
+
int val = br.receive_extend(sz);
|
| 595 |
+
int z = kZigZag[k];
|
| 596 |
+
coef[z] = val * qt[cp.qt_id][z];
|
| 597 |
+
++k;
|
| 598 |
+
}
|
| 599 |
+
|
| 600 |
+
float block[64];
|
| 601 |
+
idct_8x8(coef, block);
|
| 602 |
+
for (int by = 0; by < 8; ++by) {
|
| 603 |
+
for (int bx = 0; bx < 8; ++bx) {
|
| 604 |
+
int py = bv * 8 + by;
|
| 605 |
+
int px = bh * 8 + bx;
|
| 606 |
+
if (py < 32 && px < 32) {
|
| 607 |
+
comp_buf[c][py * cw + px] = block[by * 8 + bx];
|
| 608 |
+
}
|
| 609 |
+
}
|
| 610 |
+
}
|
| 611 |
+
}
|
| 612 |
+
}
|
| 613 |
+
}
|
| 614 |
+
|
| 615 |
+
// Convert MCU YCbCr -> ARGB32
|
| 616 |
+
for (int py = 0; py < mcu_h; ++py) {
|
| 617 |
+
int gy = my * mcu_h + py;
|
| 618 |
+
if (gy >= height) break;
|
| 619 |
+
for (int px = 0; px < mcu_w; ++px) {
|
| 620 |
+
int gx = mx * mcu_w + px;
|
| 621 |
+
if (gx >= width) break;
|
| 622 |
+
|
| 623 |
+
int yx = (px * comps[0].h_samp) / max_h;
|
| 624 |
+
int yy = (py * comps[0].v_samp) / max_v;
|
| 625 |
+
float Y = comp_buf[0][yy * (comps[0].h_samp * 8) + yx];
|
| 626 |
+
|
| 627 |
+
uint8_t r = 0, g = 0, b = 0;
|
| 628 |
+
if (num_comps == 1) {
|
| 629 |
+
uint8_t v = static_cast<uint8_t>(std::clamp(std::round(Y), 0.0f, 255.0f));
|
| 630 |
+
r = g = b = v;
|
| 631 |
+
} else {
|
| 632 |
+
int cbx = (px * comps[1].h_samp) / max_h;
|
| 633 |
+
int cby = (py * comps[1].v_samp) / max_v;
|
| 634 |
+
float Cb = comp_buf[1][cby * (comps[1].h_samp * 8) + cbx] - 128.0f;
|
| 635 |
+
|
| 636 |
+
int crx = (px * comps[2].h_samp) / max_h;
|
| 637 |
+
int cry = (py * comps[2].v_samp) / max_v;
|
| 638 |
+
float Cr = comp_buf[2][cry * (comps[2].h_samp * 8) + crx] - 128.0f;
|
| 639 |
+
|
| 640 |
+
r = static_cast<uint8_t>(std::clamp(std::round(Y + 1.402f * Cr), 0.0f, 255.0f));
|
| 641 |
+
g = static_cast<uint8_t>(std::clamp(std::round(Y - 0.344136f * Cb - 0.714136f * Cr), 0.0f, 255.0f));
|
| 642 |
+
b = static_cast<uint8_t>(std::clamp(std::round(Y + 1.772f * Cb), 0.0f, 255.0f));
|
| 643 |
+
}
|
| 644 |
+
out.argb[static_cast<size_t>(gy * width + gx)] =
|
| 645 |
+
0xFF000000u | (static_cast<uint32_t>(r) << 16) | (static_cast<uint32_t>(g) << 8) | b;
|
| 646 |
+
}
|
| 647 |
+
}
|
| 648 |
+
}
|
| 649 |
+
}
|
| 650 |
+
|
| 651 |
+
out.valid = true;
|
| 652 |
+
return out;
|
| 653 |
+
}
|
| 654 |
+
|
| 655 |
} // namespace astra
|
src/gfx/image_decoder.hpp
CHANGED
|
@@ -18,6 +18,7 @@ struct DecodedImage {
|
|
| 18 |
// Zero-build-dependency native image decoder (inspired by Firefox image/decoders/).
|
| 19 |
// Supports:
|
| 20 |
// - PNG (8-bit RGB, RGBA, Grayscale, Grayscale+Alpha, Indexed PLTE; all 5 PNG filters)
|
|
|
|
| 21 |
// - BMP (24-bit & 32-bit uncompressed DIB)
|
| 22 |
// - PPM (P6 binary RGB)
|
| 23 |
class ImageDecoder {
|
|
@@ -27,6 +28,7 @@ public:
|
|
| 27 |
|
| 28 |
private:
|
| 29 |
static DecodedImage decode_png(const uint8_t* data, size_t len);
|
|
|
|
| 30 |
static DecodedImage decode_bmp(const uint8_t* data, size_t len);
|
| 31 |
static DecodedImage decode_ppm(const uint8_t* data, size_t len);
|
| 32 |
};
|
|
|
|
| 18 |
// Zero-build-dependency native image decoder (inspired by Firefox image/decoders/).
|
| 19 |
// Supports:
|
| 20 |
// - PNG (8-bit RGB, RGBA, Grayscale, Grayscale+Alpha, Indexed PLTE; all 5 PNG filters)
|
| 21 |
+
// - JPEG (Pure C++20 Baseline Sequential DCT: Grayscale & YCbCr 4:4:4/4:2:2/4:2:0, DRI)
|
| 22 |
// - BMP (24-bit & 32-bit uncompressed DIB)
|
| 23 |
// - PPM (P6 binary RGB)
|
| 24 |
class ImageDecoder {
|
|
|
|
| 28 |
|
| 29 |
private:
|
| 30 |
static DecodedImage decode_png(const uint8_t* data, size_t len);
|
| 31 |
+
static DecodedImage decode_jpeg(const uint8_t* data, size_t len);
|
| 32 |
static DecodedImage decode_bmp(const uint8_t* data, size_t len);
|
| 33 |
static DecodedImage decode_ppm(const uint8_t* data, size_t len);
|
| 34 |
};
|
src/gfx/rasterizer.cpp
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
#include "gfx/rasterizer.hpp"
|
|
|
|
| 2 |
|
| 3 |
#include <algorithm>
|
| 4 |
#include <cmath>
|
|
@@ -202,7 +203,40 @@ void Framebuffer::stroke_rounded_rect(int x, int y, int w, int h, int radius, in
|
|
| 202 |
}
|
| 203 |
}
|
| 204 |
|
| 205 |
-
void Framebuffer::draw_text(int x, int y, std::string_view raw_text, uint32_t argb,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
std::string text = utf8_to_printable(raw_text);
|
| 207 |
int scale = (font_size >= 26.0f) ? 3 : ((font_size >= 18.0f) ? 2 : 1);
|
| 208 |
int adv = scale * 8 + (bold ? 1 : 0);
|
|
@@ -249,47 +283,47 @@ int Framebuffer::draw_browser_chrome(const ChromeStatus& st) {
|
|
| 249 |
fill_rect(0, tab_bar_h, width_, nav_bar_h, 0xFF111827u);
|
| 250 |
fill_rect(0, total_h - 1, width_, 1, 0xFF1F2937u);
|
| 251 |
|
| 252 |
-
fill_rounded_rect(10, 7,
|
| 253 |
-
draw_text(
|
| 254 |
|
| 255 |
-
int tx =
|
| 256 |
size_t max_tabs = std::min<size_t>(st.tab_titles.size(), 5);
|
| 257 |
for (size_t i = 0; i < max_tabs; ++i) {
|
| 258 |
bool active = (i == st.active_tab_index);
|
| 259 |
-
int tw =
|
| 260 |
fill_rounded_rect(tx, 6, tw, 28, 8, active ? 0xFF1E293Bu : 0xFF111827u);
|
| 261 |
if (active) fill_rect(tx + 12, 6, tw - 24, 2, 0xFF38BDF8u);
|
| 262 |
std::string lbl = st.tab_titles[i];
|
| 263 |
-
if (lbl.size() >
|
| 264 |
-
draw_text(tx +
|
| 265 |
tx += tw + 8;
|
| 266 |
-
if (tx +
|
| 267 |
}
|
| 268 |
|
| 269 |
int bar_x = 16;
|
| 270 |
-
int bar_w = std::max(240, width_ -
|
| 271 |
fill_rounded_rect(bar_x, tab_bar_h + 8, bar_w, 30, 8, 0xFF0B1120u);
|
| 272 |
stroke_rounded_rect(bar_x, tab_bar_h + 8, bar_w, 30, 8, 1,
|
| 273 |
st.tls_verified ? 0xFF10B981u : 0xFF334155u);
|
| 274 |
std::string url_disp = (st.tls_verified ? "[TLS] " : "") + st.active_url;
|
| 275 |
-
draw_text(bar_x + 12, tab_bar_h +
|
| 276 |
|
| 277 |
int rx = bar_x + bar_w + 12;
|
| 278 |
std::string shield_txt = "SHIELD:" + std::to_string(st.trackers_blocked);
|
| 279 |
-
fill_rounded_rect(rx, tab_bar_h + 8,
|
| 280 |
-
draw_text(rx + 12, tab_bar_h +
|
| 281 |
|
| 282 |
-
rx +=
|
| 283 |
std::string ram_txt = "RAM:" + std::to_string(st.rss_mb) + "/" + std::to_string(st.ceiling_mb) + "MB";
|
| 284 |
uint32_t ram_bg = (st.rss_mb * 100 / std::max<uint64_t>(1, st.ceiling_mb) > 80) ? 0xFF7F1D1Du : 0xFF1E293Bu;
|
| 285 |
-
fill_rounded_rect(rx, tab_bar_h + 8,
|
| 286 |
-
draw_text(rx + 12, tab_bar_h +
|
| 287 |
|
| 288 |
-
rx +=
|
| 289 |
-
if (rx +
|
| 290 |
std::string ext_txt = "EXT:" + std::to_string(st.extensions_loaded);
|
| 291 |
-
fill_rounded_rect(rx, tab_bar_h + 8,
|
| 292 |
-
draw_text(rx + 12, tab_bar_h +
|
| 293 |
}
|
| 294 |
|
| 295 |
return total_h;
|
|
@@ -328,9 +362,33 @@ void Framebuffer::composite_display_list(const DisplayList& dl,
|
|
| 328 |
}
|
| 329 |
case DisplayItemType::DrawText:
|
| 330 |
if (iy >= y_offset) {
|
| 331 |
-
draw_text(ix, iy, item.text, item.color_argb, item.font_size, item.bold);
|
| 332 |
}
|
| 333 |
break;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 334 |
case DisplayItemType::DrawImage: {
|
| 335 |
int cx = std::max(0, ix);
|
| 336 |
int cy = std::max(y_offset, iy);
|
|
@@ -342,7 +400,7 @@ void Framebuffer::composite_display_list(const DisplayList& dl,
|
|
| 342 |
stroke_rounded_rect(cx, cy, cw, ch, 6, 1, 0xFF334155u);
|
| 343 |
} else {
|
| 344 |
fill_rounded_rect(cx, cy, cw, ch, 6, 0xFF1E293Bu);
|
| 345 |
-
draw_text(cx + 8, cy + ch / 2 -
|
| 346 |
}
|
| 347 |
break;
|
| 348 |
}
|
|
@@ -356,8 +414,9 @@ void Framebuffer::composite_display_list(const DisplayList& dl,
|
|
| 356 |
item.text, time3d_sec, cam3d);
|
| 357 |
stroke_rounded_rect(cx, cy, cw, ch, 6, 1, 0xFF38BDF8u);
|
| 358 |
std::string hud = "Astra3D Smooth | " + std::to_string(st3d.triangles_rasterized) + " tris";
|
| 359 |
-
|
| 360 |
-
|
|
|
|
| 361 |
}
|
| 362 |
break;
|
| 363 |
}
|
|
@@ -371,13 +430,13 @@ void Framebuffer::composite_display_list(const DisplayList& dl,
|
|
| 371 |
frame.width > 0 && frame.height > 0 && !frame.argb.empty()) {
|
| 372 |
blit_scaled(cx, cy, cw, ch, frame.argb.data(), frame.width, frame.height);
|
| 373 |
stroke_rounded_rect(cx, cy, cw, ch, 6, 1, 0xFF34D399u);
|
| 374 |
-
int pill_w = std::min(cw - 20, static_cast<int>(frame.label.
|
| 375 |
-
fill_rounded_rect(cx + 10, cy + ch - 28, pill_w,
|
| 376 |
-
draw_text(cx + 18, cy + ch -
|
| 377 |
} else {
|
| 378 |
fill_rounded_rect(cx, cy, cw, ch, 8, 0xFF0F172Au);
|
| 379 |
stroke_rounded_rect(cx, cy, cw, ch, 8, 1, 0xFF475569u);
|
| 380 |
-
draw_text(cx + 16, cy + ch / 2 - 6, "AstraMedia Video: " + item.text, 0xFF94A3B8u, 13.0f, true);
|
| 381 |
}
|
| 382 |
break;
|
| 383 |
}
|
|
@@ -535,8 +594,10 @@ bool Framebuffer::present_x11_interactive(const std::string& window_title,
|
|
| 535 |
}
|
| 536 |
if (callbacks.on_repaint) callbacks.on_repaint(*this);
|
| 537 |
blit_now();
|
| 538 |
-
} else if (ev.type == 2) { // KeyPress (Escape=9,
|
| 539 |
-
|
|
|
|
|
|
|
| 540 |
running = false;
|
| 541 |
} else if (ev.xkey.keycode == 116 && callbacks.on_scroll) {
|
| 542 |
callbacks.on_scroll(80);
|
|
@@ -546,6 +607,23 @@ bool Framebuffer::present_x11_interactive(const std::string& window_title,
|
|
| 546 |
callbacks.on_scroll(-80);
|
| 547 |
if (callbacks.on_repaint) callbacks.on_repaint(*this);
|
| 548 |
blit_now();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 549 |
}
|
| 550 |
}
|
| 551 |
}
|
|
|
|
| 1 |
#include "gfx/rasterizer.hpp"
|
| 2 |
+
#include "gfx/font_engine.hpp"
|
| 3 |
|
| 4 |
#include <algorithm>
|
| 5 |
#include <cmath>
|
|
|
|
| 203 |
}
|
| 204 |
}
|
| 205 |
|
| 206 |
+
void Framebuffer::draw_text(int x, int y, std::string_view raw_text, uint32_t argb,
|
| 207 |
+
float font_size, bool bold, bool monospace) {
|
| 208 |
+
auto& fe = FontEngine::instance();
|
| 209 |
+
if (fe.freetype_active()) {
|
| 210 |
+
int px = static_cast<int>(std::round(std::clamp(font_size, 8.0f, 72.0f)));
|
| 211 |
+
int baseline_y = y + static_cast<int>(std::round(px * 0.88f));
|
| 212 |
+
uint32_t base_a = (argb >> 24) & 0xFFu;
|
| 213 |
+
uint32_t rgb = argb & 0x00FFFFFFu;
|
| 214 |
+
int cx = x;
|
| 215 |
+
size_t idx = 0;
|
| 216 |
+
while (idx < raw_text.size()) {
|
| 217 |
+
uint32_t cp = FontEngine::next_utf8_codepoint(raw_text, idx);
|
| 218 |
+
const CachedGlyph* g = fe.get_glyph(cp, px, bold, monospace);
|
| 219 |
+
if (!g) { cx += px / 2; continue; }
|
| 220 |
+
int gx0 = cx + g->bearing_x;
|
| 221 |
+
int gy0 = baseline_y - g->bearing_y;
|
| 222 |
+
for (int row = 0; row < g->height; ++row) {
|
| 223 |
+
int py = gy0 + row;
|
| 224 |
+
if (py < 0 || py >= height_) continue;
|
| 225 |
+
for (int col = 0; col < g->width; ++col) {
|
| 226 |
+
uint8_t cov = g->alpha[static_cast<size_t>(row * g->width + col)];
|
| 227 |
+
if (cov == 0) continue;
|
| 228 |
+
int pxx = gx0 + col;
|
| 229 |
+
if (pxx < 0 || pxx >= width_) continue;
|
| 230 |
+
uint32_t eff_a = (base_a * cov) / 255u;
|
| 231 |
+
if (eff_a > 0) blend_pixel(pxx, py, (eff_a << 24) | rgb);
|
| 232 |
+
}
|
| 233 |
+
}
|
| 234 |
+
cx += g->advance_x;
|
| 235 |
+
if (cx > width_) break;
|
| 236 |
+
}
|
| 237 |
+
return;
|
| 238 |
+
}
|
| 239 |
+
|
| 240 |
std::string text = utf8_to_printable(raw_text);
|
| 241 |
int scale = (font_size >= 26.0f) ? 3 : ((font_size >= 18.0f) ? 2 : 1);
|
| 242 |
int adv = scale * 8 + (bold ? 1 : 0);
|
|
|
|
| 283 |
fill_rect(0, tab_bar_h, width_, nav_bar_h, 0xFF111827u);
|
| 284 |
fill_rect(0, total_h - 1, width_, 1, 0xFF1F2937u);
|
| 285 |
|
| 286 |
+
fill_rounded_rect(10, 7, 72, 24, 6, 0xFF0284C7u);
|
| 287 |
+
draw_text(20, 12, "ASTRA", 0xFFFFFFFFu, 13.0f, true, false);
|
| 288 |
|
| 289 |
+
int tx = 92;
|
| 290 |
size_t max_tabs = std::min<size_t>(st.tab_titles.size(), 5);
|
| 291 |
for (size_t i = 0; i < max_tabs; ++i) {
|
| 292 |
bool active = (i == st.active_tab_index);
|
| 293 |
+
int tw = 205;
|
| 294 |
fill_rounded_rect(tx, 6, tw, 28, 8, active ? 0xFF1E293Bu : 0xFF111827u);
|
| 295 |
if (active) fill_rect(tx + 12, 6, tw - 24, 2, 0xFF38BDF8u);
|
| 296 |
std::string lbl = st.tab_titles[i];
|
| 297 |
+
if (lbl.size() > 24) lbl = lbl.substr(0, 22) + "..";
|
| 298 |
+
draw_text(tx + 12, 13, lbl, active ? 0xFFF8FAFCu : 0xFF94A3B8u, 12.0f, active, false);
|
| 299 |
tx += tw + 8;
|
| 300 |
+
if (tx + 205 > width_) break;
|
| 301 |
}
|
| 302 |
|
| 303 |
int bar_x = 16;
|
| 304 |
+
int bar_w = std::max(240, width_ - 416);
|
| 305 |
fill_rounded_rect(bar_x, tab_bar_h + 8, bar_w, 30, 8, 0xFF0B1120u);
|
| 306 |
stroke_rounded_rect(bar_x, tab_bar_h + 8, bar_w, 30, 8, 1,
|
| 307 |
st.tls_verified ? 0xFF10B981u : 0xFF334155u);
|
| 308 |
std::string url_disp = (st.tls_verified ? "[TLS] " : "") + st.active_url;
|
| 309 |
+
draw_text(bar_x + 12, tab_bar_h + 15, url_disp, 0xFFE2E8F0u, 13.0f, false, true);
|
| 310 |
|
| 311 |
int rx = bar_x + bar_w + 12;
|
| 312 |
std::string shield_txt = "SHIELD:" + std::to_string(st.trackers_blocked);
|
| 313 |
+
fill_rounded_rect(rx, tab_bar_h + 8, 108, 30, 8, 0xFF064E3Bu);
|
| 314 |
+
draw_text(rx + 12, tab_bar_h + 15, shield_txt, 0xFF6EE7B7u, 12.0f, true, true);
|
| 315 |
|
| 316 |
+
rx += 116;
|
| 317 |
std::string ram_txt = "RAM:" + std::to_string(st.rss_mb) + "/" + std::to_string(st.ceiling_mb) + "MB";
|
| 318 |
uint32_t ram_bg = (st.rss_mb * 100 / std::max<uint64_t>(1, st.ceiling_mb) > 80) ? 0xFF7F1D1Du : 0xFF1E293Bu;
|
| 319 |
+
fill_rounded_rect(rx, tab_bar_h + 8, 180, 30, 8, ram_bg);
|
| 320 |
+
draw_text(rx + 12, tab_bar_h + 15, ram_txt, 0xFF38BDF8u, 12.0f, true, true);
|
| 321 |
|
| 322 |
+
rx += 188;
|
| 323 |
+
if (rx + 76 <= width_) {
|
| 324 |
std::string ext_txt = "EXT:" + std::to_string(st.extensions_loaded);
|
| 325 |
+
fill_rounded_rect(rx, tab_bar_h + 8, 76, 30, 8, 0xFF312E81u);
|
| 326 |
+
draw_text(rx + 12, tab_bar_h + 15, ext_txt, 0xFFC7D2FEu, 12.0f, true, true);
|
| 327 |
}
|
| 328 |
|
| 329 |
return total_h;
|
|
|
|
| 362 |
}
|
| 363 |
case DisplayItemType::DrawText:
|
| 364 |
if (iy >= y_offset) {
|
| 365 |
+
draw_text(ix, iy, item.text, item.color_argb, item.font_size, item.bold, item.monospace);
|
| 366 |
}
|
| 367 |
break;
|
| 368 |
+
case DisplayItemType::DrawInput: {
|
| 369 |
+
int cy = std::max(y_offset, iy);
|
| 370 |
+
int ch = std::min(height_, iy + ih) - cy;
|
| 371 |
+
if (ch > 0) {
|
| 372 |
+
int rad = static_cast<int>(item.radius);
|
| 373 |
+
fill_rounded_rect(ix, cy, iw, ch, rad, item.focused ? 0xFF0F1B2Eu : 0xFF0B1120u);
|
| 374 |
+
stroke_rounded_rect(ix, cy, iw, ch, rad, item.focused ? 2 : 1,
|
| 375 |
+
item.focused ? 0xFF38BDF8u : 0xFF334155u);
|
| 376 |
+
int ty = iy + std::max(4, (ih - static_cast<int>(item.font_size)) / 2 - 1);
|
| 377 |
+
if (ty >= y_offset && ty < height_ - 4) {
|
| 378 |
+
draw_text(ix + 12, ty, item.text, item.color_argb, item.font_size, false, false);
|
| 379 |
+
if (item.focused) {
|
| 380 |
+
float tw = (item.color_argb == 0xFF64748Bu)
|
| 381 |
+
? 0.0f
|
| 382 |
+
: FontEngine::instance().measure_text(item.text, item.font_size, false, false);
|
| 383 |
+
int caret_x = ix + 13 + static_cast<int>(std::round(tw));
|
| 384 |
+
if (caret_x < ix + iw - 6) {
|
| 385 |
+
fill_rect(caret_x, ty, 2, static_cast<int>(item.font_size) + 2, 0xFF38BDF8u);
|
| 386 |
+
}
|
| 387 |
+
}
|
| 388 |
+
}
|
| 389 |
+
}
|
| 390 |
+
break;
|
| 391 |
+
}
|
| 392 |
case DisplayItemType::DrawImage: {
|
| 393 |
int cx = std::max(0, ix);
|
| 394 |
int cy = std::max(y_offset, iy);
|
|
|
|
| 400 |
stroke_rounded_rect(cx, cy, cw, ch, 6, 1, 0xFF334155u);
|
| 401 |
} else {
|
| 402 |
fill_rounded_rect(cx, cy, cw, ch, 6, 0xFF1E293Bu);
|
| 403 |
+
draw_text(cx + 8, cy + ch / 2 - 6, "[IMG] " + item.text, 0xFF94A3B8u, 11.0f, false, true);
|
| 404 |
}
|
| 405 |
break;
|
| 406 |
}
|
|
|
|
| 414 |
item.text, time3d_sec, cam3d);
|
| 415 |
stroke_rounded_rect(cx, cy, cw, ch, 6, 1, 0xFF38BDF8u);
|
| 416 |
std::string hud = "Astra3D Smooth | " + std::to_string(st3d.triangles_rasterized) + " tris";
|
| 417 |
+
int hud_w = std::min(cw - 20, static_cast<int>(FontEngine::instance().measure_text(hud, 11.0f, true, true)) + 20);
|
| 418 |
+
fill_rounded_rect(cx + 10, cy + ch - 28, hud_w, 22, 4, 0xCC0A0F1Au);
|
| 419 |
+
draw_text(cx + 18, cy + ch - 23, hud, 0xFF7DD3FCu, 11.0f, true, true);
|
| 420 |
}
|
| 421 |
break;
|
| 422 |
}
|
|
|
|
| 430 |
frame.width > 0 && frame.height > 0 && !frame.argb.empty()) {
|
| 431 |
blit_scaled(cx, cy, cw, ch, frame.argb.data(), frame.width, frame.height);
|
| 432 |
stroke_rounded_rect(cx, cy, cw, ch, 6, 1, 0xFF34D399u);
|
| 433 |
+
int pill_w = std::min(cw - 20, static_cast<int>(FontEngine::instance().measure_text(frame.label, 11.0f, true, true)) + 20);
|
| 434 |
+
fill_rounded_rect(cx + 10, cy + ch - 28, pill_w, 22, 4, 0xCC06141Eu);
|
| 435 |
+
draw_text(cx + 18, cy + ch - 23, frame.label, 0xFF6EE7B7u, 11.0f, true, true);
|
| 436 |
} else {
|
| 437 |
fill_rounded_rect(cx, cy, cw, ch, 8, 0xFF0F172Au);
|
| 438 |
stroke_rounded_rect(cx, cy, cw, ch, 8, 1, 0xFF475569u);
|
| 439 |
+
draw_text(cx + 16, cy + ch / 2 - 6, "AstraMedia Video: " + item.text, 0xFF94A3B8u, 13.0f, true, false);
|
| 440 |
}
|
| 441 |
break;
|
| 442 |
}
|
|
|
|
| 594 |
}
|
| 595 |
if (callbacks.on_repaint) callbacks.on_repaint(*this);
|
| 596 |
blit_now();
|
| 597 |
+
} else if (ev.type == 2) { // KeyPress (Escape=9, Backspace=22, Enter=36, Down=116, Up=111)
|
| 598 |
+
using XLookupString_fn = int (*)(void*, char*, int, unsigned long*, void*);
|
| 599 |
+
static auto XLookupString = reinterpret_cast<XLookupString_fn>(dlsym(x11, "XLookupString"));
|
| 600 |
+
if (ev.xkey.keycode == 9) {
|
| 601 |
running = false;
|
| 602 |
} else if (ev.xkey.keycode == 116 && callbacks.on_scroll) {
|
| 603 |
callbacks.on_scroll(80);
|
|
|
|
| 607 |
callbacks.on_scroll(-80);
|
| 608 |
if (callbacks.on_repaint) callbacks.on_repaint(*this);
|
| 609 |
blit_now();
|
| 610 |
+
} else if (ev.xkey.keycode == 22 && callbacks.on_backspace) {
|
| 611 |
+
callbacks.on_backspace();
|
| 612 |
+
if (callbacks.on_repaint) callbacks.on_repaint(*this);
|
| 613 |
+
blit_now();
|
| 614 |
+
} else if (ev.xkey.keycode == 36 && callbacks.on_enter) {
|
| 615 |
+
callbacks.on_enter();
|
| 616 |
+
if (callbacks.on_repaint) callbacks.on_repaint(*this);
|
| 617 |
+
blit_now();
|
| 618 |
+
} else if (XLookupString && callbacks.on_text) {
|
| 619 |
+
char buf[16] = {};
|
| 620 |
+
unsigned long ks = 0;
|
| 621 |
+
int n = XLookupString(&ev.xkey, buf, sizeof(buf) - 1, &ks, nullptr);
|
| 622 |
+
if (n > 0 && buf[0] >= 32 && buf[0] < 127) {
|
| 623 |
+
callbacks.on_text(std::string(buf, static_cast<size_t>(n)));
|
| 624 |
+
if (callbacks.on_repaint) callbacks.on_repaint(*this);
|
| 625 |
+
blit_now();
|
| 626 |
+
}
|
| 627 |
}
|
| 628 |
}
|
| 629 |
}
|
src/gfx/rasterizer.hpp
CHANGED
|
@@ -36,12 +36,15 @@ class Framebuffer;
|
|
| 36 |
struct InteractiveCallbacks {
|
| 37 |
std::function<void(int x, int y)> on_click;
|
| 38 |
std::function<void(int delta_y)> on_scroll;
|
|
|
|
|
|
|
|
|
|
| 39 |
std::function<void(Framebuffer& fb)> on_repaint;
|
| 40 |
};
|
| 41 |
|
| 42 |
-
// Astra Compositor
|
| 43 |
-
// native <img> blitting,
|
| 44 |
-
// and interactive Linux X11 desktop
|
| 45 |
class Framebuffer {
|
| 46 |
public:
|
| 47 |
static constexpr int kChromeHeight = 84;
|
|
@@ -58,7 +61,8 @@ public:
|
|
| 58 |
void fill_rect(int x, int y, int w, int h, uint32_t argb);
|
| 59 |
void fill_rounded_rect(int x, int y, int w, int h, int radius, uint32_t argb);
|
| 60 |
void stroke_rounded_rect(int x, int y, int w, int h, int radius, int thickness, uint32_t argb);
|
| 61 |
-
void draw_text(int x, int y, std::string_view text, uint32_t argb, float font_size,
|
|
|
|
| 62 |
void blit_scaled(int dst_x, int dst_y, int dst_w, int dst_h,
|
| 63 |
const uint32_t* src, int src_w, int src_h);
|
| 64 |
|
|
|
|
| 36 |
struct InteractiveCallbacks {
|
| 37 |
std::function<void(int x, int y)> on_click;
|
| 38 |
std::function<void(int delta_y)> on_scroll;
|
| 39 |
+
std::function<void(const std::string& text)> on_text;
|
| 40 |
+
std::function<void()> on_backspace;
|
| 41 |
+
std::function<void()> on_enter;
|
| 42 |
std::function<void(Framebuffer& fb)> on_repaint;
|
| 43 |
};
|
| 44 |
|
| 45 |
+
// Astra Compositor v3: ARGB32 software compositor with Anti-Aliased TrueType (AstraFont),
|
| 46 |
+
// APZ-style scroll_y, native PNG/JPEG/BMP/PPM <img> blitting, interactive <input>,
|
| 47 |
+
// smooth Astra3D integration, zero-dep PNG encoder, and interactive Linux X11 desktop window.
|
| 48 |
class Framebuffer {
|
| 49 |
public:
|
| 50 |
static constexpr int kChromeHeight = 84;
|
|
|
|
| 61 |
void fill_rect(int x, int y, int w, int h, uint32_t argb);
|
| 62 |
void fill_rounded_rect(int x, int y, int w, int h, int radius, uint32_t argb);
|
| 63 |
void stroke_rounded_rect(int x, int y, int w, int h, int radius, int thickness, uint32_t argb);
|
| 64 |
+
void draw_text(int x, int y, std::string_view text, uint32_t argb, float font_size,
|
| 65 |
+
bool bold = false, bool monospace = false);
|
| 66 |
void blit_scaled(int dst_x, int dst_y, int dst_w, int dst_h,
|
| 67 |
const uint32_t* src, int src_w, int src_h);
|
| 68 |
|
src/js/astra_js.cpp
CHANGED
|
@@ -1,9 +1,11 @@
|
|
| 1 |
#include "js/astra_js.hpp"
|
|
|
|
| 2 |
|
| 3 |
#include <algorithm>
|
| 4 |
#include <cctype>
|
| 5 |
#include <cmath>
|
| 6 |
#include <cstdlib>
|
|
|
|
| 7 |
#include <sstream>
|
| 8 |
|
| 9 |
namespace astra {
|
|
@@ -639,6 +641,10 @@ void AstraJsEngine::bind_storage(StorageGetFn getter, StorageSetFn setter) {
|
|
| 639 |
storage_set_ = std::move(setter);
|
| 640 |
}
|
| 641 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 642 |
GcObject* AstraJsEngine::alloc_object(GcObject::Type type) {
|
| 643 |
GcObject* o = new GcObject();
|
| 644 |
o->type = type;
|
|
@@ -711,6 +717,24 @@ JsValue AstraJsEngine::invoke_callable(const JsValue& fn,
|
|
| 711 |
if (fn.sval == "__gc__") {
|
| 712 |
return JsValue::make_number(static_cast<double>(gc_collect()));
|
| 713 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 714 |
return JsValue::make_undefined();
|
| 715 |
}
|
| 716 |
|
|
@@ -760,7 +784,8 @@ JsValue AstraJsEngine::run_chunk(const BytecodeChunk& chunk,
|
|
| 760 |
}
|
| 761 |
case OpCode::LoadGlobal: {
|
| 762 |
const std::string& name = chunk.names[static_cast<size_t>(ins.operand)];
|
| 763 |
-
if (name == "document" || name == "console" || name == "astra" ||
|
|
|
|
| 764 |
stack_.push_back(JsValue::make_string("__builtin_" + name + "__"));
|
| 765 |
} else if (name == "queueMicrotask") {
|
| 766 |
stack_.push_back(JsValue::make_string("__queueMicrotask__"));
|
|
@@ -768,6 +793,8 @@ JsValue AstraJsEngine::run_chunk(const BytecodeChunk& chunk,
|
|
| 768 |
stack_.push_back(JsValue::make_string("__setTimeout__"));
|
| 769 |
} else if (name == "gc") {
|
| 770 |
stack_.push_back(JsValue::make_string("__gc__"));
|
|
|
|
|
|
|
| 771 |
} else {
|
| 772 |
auto it = globals_.find(name);
|
| 773 |
stack_.push_back(it != globals_.end() ? it->second : JsValue::make_undefined());
|
|
@@ -937,7 +964,120 @@ JsValue AstraJsEngine::run_chunk(const BytecodeChunk& chunk,
|
|
| 937 |
}
|
| 938 |
} else if (recv.kind == JsValue::Kind::String && recv.sval == "__builtin_Math__") {
|
| 939 |
if (method == "floor" && !args.empty()) ret = JsValue::make_number(std::floor(args[0].to_number()));
|
|
|
|
|
|
|
| 940 |
else if (method == "sqrt" && !args.empty()) ret = JsValue::make_number(std::sqrt(args[0].to_number()));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 941 |
} else if (recv.kind == JsValue::Kind::DomRef && recv.node) {
|
| 942 |
if (method == "setAttribute" && args.size() >= 2) {
|
| 943 |
recv.node->set_attr(args[0].to_string(), args[1].to_string());
|
|
@@ -954,6 +1094,37 @@ JsValue AstraJsEngine::run_chunk(const BytecodeChunk& chunk,
|
|
| 954 |
if (recv.obj->type == GcObject::Type::Array && method == "push" && !args.empty()) {
|
| 955 |
recv.obj->elements.push_back(args[0]);
|
| 956 |
ret = JsValue::make_number(static_cast<double>(recv.obj->elements.size()));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 957 |
} else if (recv.obj->props.count(method) > 0) {
|
| 958 |
ret = invoke_callable(recv.obj->props[method], args, doc, rep);
|
| 959 |
}
|
|
|
|
| 1 |
#include "js/astra_js.hpp"
|
| 2 |
+
#include "core/json.hpp"
|
| 3 |
|
| 4 |
#include <algorithm>
|
| 5 |
#include <cctype>
|
| 6 |
#include <cmath>
|
| 7 |
#include <cstdlib>
|
| 8 |
+
#include <functional>
|
| 9 |
#include <sstream>
|
| 10 |
|
| 11 |
namespace astra {
|
|
|
|
| 641 |
storage_set_ = std::move(setter);
|
| 642 |
}
|
| 643 |
|
| 644 |
+
void AstraJsEngine::bind_fetch(FetchFn fetcher) {
|
| 645 |
+
fetch_fn_ = std::move(fetcher);
|
| 646 |
+
}
|
| 647 |
+
|
| 648 |
GcObject* AstraJsEngine::alloc_object(GcObject::Type type) {
|
| 649 |
GcObject* o = new GcObject();
|
| 650 |
o->type = type;
|
|
|
|
| 717 |
if (fn.sval == "__gc__") {
|
| 718 |
return JsValue::make_number(static_cast<double>(gc_collect()));
|
| 719 |
}
|
| 720 |
+
if (fn.sval == "__fetch__" && !args.empty()) {
|
| 721 |
+
std::string url = args[0].to_string();
|
| 722 |
+
JsFetchResult fr;
|
| 723 |
+
if (fetch_fn_) {
|
| 724 |
+
fr = fetch_fn_(url);
|
| 725 |
+
} else {
|
| 726 |
+
fr.error = "No network transport bound";
|
| 727 |
+
}
|
| 728 |
+
GcObject* resp = alloc_object(GcObject::Type::Map);
|
| 729 |
+
resp->props["ok"] = JsValue::make_bool(fr.ok);
|
| 730 |
+
resp->props["status"] = JsValue::make_number(static_cast<double>(fr.status));
|
| 731 |
+
resp->props["body"] = JsValue::make_string(fr.body);
|
| 732 |
+
resp->props["error"] = JsValue::make_string(fr.error);
|
| 733 |
+
resp->props["fromCache"] = JsValue::make_bool(fr.from_cache);
|
| 734 |
+
resp->props["blockedByShield"] = JsValue::make_bool(fr.blocked_by_shield);
|
| 735 |
+
resp->props["__is_fetch_resp__"] = JsValue::make_bool(true);
|
| 736 |
+
return JsValue::make_obj(resp);
|
| 737 |
+
}
|
| 738 |
return JsValue::make_undefined();
|
| 739 |
}
|
| 740 |
|
|
|
|
| 784 |
}
|
| 785 |
case OpCode::LoadGlobal: {
|
| 786 |
const std::string& name = chunk.names[static_cast<size_t>(ins.operand)];
|
| 787 |
+
if (name == "document" || name == "console" || name == "astra" ||
|
| 788 |
+
name == "Math" || name == "JSON" || name == "localStorage") {
|
| 789 |
stack_.push_back(JsValue::make_string("__builtin_" + name + "__"));
|
| 790 |
} else if (name == "queueMicrotask") {
|
| 791 |
stack_.push_back(JsValue::make_string("__queueMicrotask__"));
|
|
|
|
| 793 |
stack_.push_back(JsValue::make_string("__setTimeout__"));
|
| 794 |
} else if (name == "gc") {
|
| 795 |
stack_.push_back(JsValue::make_string("__gc__"));
|
| 796 |
+
} else if (name == "fetch") {
|
| 797 |
+
stack_.push_back(JsValue::make_string("__fetch__"));
|
| 798 |
} else {
|
| 799 |
auto it = globals_.find(name);
|
| 800 |
stack_.push_back(it != globals_.end() ? it->second : JsValue::make_undefined());
|
|
|
|
| 964 |
}
|
| 965 |
} else if (recv.kind == JsValue::Kind::String && recv.sval == "__builtin_Math__") {
|
| 966 |
if (method == "floor" && !args.empty()) ret = JsValue::make_number(std::floor(args[0].to_number()));
|
| 967 |
+
else if (method == "ceil" && !args.empty()) ret = JsValue::make_number(std::ceil(args[0].to_number()));
|
| 968 |
+
else if (method == "round" && !args.empty()) ret = JsValue::make_number(std::round(args[0].to_number()));
|
| 969 |
else if (method == "sqrt" && !args.empty()) ret = JsValue::make_number(std::sqrt(args[0].to_number()));
|
| 970 |
+
} else if (recv.kind == JsValue::Kind::String && recv.sval == "__builtin_localStorage__") {
|
| 971 |
+
if (method == "setItem" && args.size() >= 2) {
|
| 972 |
+
std::string k = args[0].to_string();
|
| 973 |
+
std::string v = args[1].to_string();
|
| 974 |
+
local_storage_[k] = v;
|
| 975 |
+
if (storage_set_) storage_set_("ls:" + k, v);
|
| 976 |
+
} else if (method == "getItem" && !args.empty()) {
|
| 977 |
+
std::string k = args[0].to_string();
|
| 978 |
+
auto it = local_storage_.find(k);
|
| 979 |
+
if (it != local_storage_.end()) ret = JsValue::make_string(it->second);
|
| 980 |
+
else if (storage_get_) {
|
| 981 |
+
std::string v = storage_get_("ls:" + k);
|
| 982 |
+
ret = v.empty() ? JsValue::make_null() : JsValue::make_string(v);
|
| 983 |
+
} else {
|
| 984 |
+
ret = JsValue::make_null();
|
| 985 |
+
}
|
| 986 |
+
} else if (method == "removeItem" && !args.empty()) {
|
| 987 |
+
local_storage_.erase(args[0].to_string());
|
| 988 |
+
}
|
| 989 |
+
} else if (recv.kind == JsValue::Kind::String && recv.sval == "__builtin_JSON__") {
|
| 990 |
+
std::function<JsValue(const JsonValue&)> json_to_js = [&](const JsonValue& jv) -> JsValue {
|
| 991 |
+
switch (jv.type()) {
|
| 992 |
+
case JsonValue::Type::Null: return JsValue::make_null();
|
| 993 |
+
case JsonValue::Type::Bool: return JsValue::make_bool(jv.as_bool());
|
| 994 |
+
case JsonValue::Type::Number: return JsValue::make_number(jv.as_number());
|
| 995 |
+
case JsonValue::Type::String: return JsValue::make_string(jv.as_string());
|
| 996 |
+
case JsonValue::Type::Array: {
|
| 997 |
+
GcObject* arr = alloc_object(GcObject::Type::Array);
|
| 998 |
+
for (const auto& el : jv.as_array()) arr->elements.push_back(json_to_js(el));
|
| 999 |
+
return JsValue::make_obj(arr);
|
| 1000 |
+
}
|
| 1001 |
+
case JsonValue::Type::Object: {
|
| 1002 |
+
GcObject* o = alloc_object(GcObject::Type::Map);
|
| 1003 |
+
for (const auto& [k, v] : jv.as_object()) o->props[k] = json_to_js(v);
|
| 1004 |
+
return JsValue::make_obj(o);
|
| 1005 |
+
}
|
| 1006 |
+
}
|
| 1007 |
+
return JsValue::make_null();
|
| 1008 |
+
};
|
| 1009 |
+
std::function<std::string(const JsValue&)> js_to_json = [&](const JsValue& v) -> std::string {
|
| 1010 |
+
switch (v.kind) {
|
| 1011 |
+
case JsValue::Kind::Undefined:
|
| 1012 |
+
case JsValue::Kind::Null: return "null";
|
| 1013 |
+
case JsValue::Kind::Bool: return v.bval ? "true" : "false";
|
| 1014 |
+
case JsValue::Kind::Number: return v.to_string();
|
| 1015 |
+
case JsValue::Kind::String: {
|
| 1016 |
+
std::string out = "\"";
|
| 1017 |
+
for (char c : v.sval) {
|
| 1018 |
+
if (c == '"' || c == '\\') { out.push_back('\\'); out.push_back(c); }
|
| 1019 |
+
else if (c == '\n') out += "\\n";
|
| 1020 |
+
else out.push_back(c);
|
| 1021 |
+
}
|
| 1022 |
+
out.push_back('"');
|
| 1023 |
+
return out;
|
| 1024 |
+
}
|
| 1025 |
+
case JsValue::Kind::DomRef: return "\"<dom>\"";
|
| 1026 |
+
case JsValue::Kind::Object:
|
| 1027 |
+
if (!v.obj) return "null";
|
| 1028 |
+
if (v.obj->type == GcObject::Type::Array) {
|
| 1029 |
+
std::string out = "[";
|
| 1030 |
+
for (size_t i = 0; i < v.obj->elements.size(); ++i) {
|
| 1031 |
+
if (i) out.push_back(',');
|
| 1032 |
+
out += js_to_json(v.obj->elements[i]);
|
| 1033 |
+
}
|
| 1034 |
+
out.push_back(']');
|
| 1035 |
+
return out;
|
| 1036 |
+
} else {
|
| 1037 |
+
std::string out = "{";
|
| 1038 |
+
bool first = true;
|
| 1039 |
+
for (const auto& [k, val] : v.obj->props) {
|
| 1040 |
+
if (k.rfind("__", 0) == 0) continue;
|
| 1041 |
+
if (!first) out.push_back(',');
|
| 1042 |
+
first = false;
|
| 1043 |
+
out += "\"" + k + "\":" + js_to_json(val);
|
| 1044 |
+
}
|
| 1045 |
+
out.push_back('}');
|
| 1046 |
+
return out;
|
| 1047 |
+
}
|
| 1048 |
+
}
|
| 1049 |
+
return "null";
|
| 1050 |
+
};
|
| 1051 |
+
if (method == "parse" && !args.empty()) {
|
| 1052 |
+
JsonValue parsed;
|
| 1053 |
+
if (JsonValue::parse(args[0].to_string(), parsed)) {
|
| 1054 |
+
ret = json_to_js(parsed);
|
| 1055 |
+
} else {
|
| 1056 |
+
ret = JsValue::make_null();
|
| 1057 |
+
}
|
| 1058 |
+
} else if (method == "stringify" && !args.empty()) {
|
| 1059 |
+
ret = JsValue::make_string(js_to_json(args[0]));
|
| 1060 |
+
}
|
| 1061 |
+
} else if (recv.kind == JsValue::Kind::String) {
|
| 1062 |
+
const std::string& s = recv.sval;
|
| 1063 |
+
if (method == "trim") {
|
| 1064 |
+
size_t a = 0, b = s.size();
|
| 1065 |
+
while (a < b && std::isspace(static_cast<unsigned char>(s[a]))) ++a;
|
| 1066 |
+
while (b > a && std::isspace(static_cast<unsigned char>(s[b - 1]))) --b;
|
| 1067 |
+
ret = JsValue::make_string(s.substr(a, b - a));
|
| 1068 |
+
} else if (method == "toUpperCase") {
|
| 1069 |
+
std::string u = s;
|
| 1070 |
+
std::transform(u.begin(), u.end(), u.begin(),
|
| 1071 |
+
[](unsigned char c) { return static_cast<char>(std::toupper(c)); });
|
| 1072 |
+
ret = JsValue::make_string(std::move(u));
|
| 1073 |
+
} else if (method == "toLowerCase") {
|
| 1074 |
+
std::string u = s;
|
| 1075 |
+
std::transform(u.begin(), u.end(), u.begin(),
|
| 1076 |
+
[](unsigned char c) { return static_cast<char>(std::tolower(c)); });
|
| 1077 |
+
ret = JsValue::make_string(std::move(u));
|
| 1078 |
+
} else if (method == "includes" && !args.empty()) {
|
| 1079 |
+
ret = JsValue::make_bool(s.find(args[0].to_string()) != std::string::npos);
|
| 1080 |
+
}
|
| 1081 |
} else if (recv.kind == JsValue::Kind::DomRef && recv.node) {
|
| 1082 |
if (method == "setAttribute" && args.size() >= 2) {
|
| 1083 |
recv.node->set_attr(args[0].to_string(), args[1].to_string());
|
|
|
|
| 1094 |
if (recv.obj->type == GcObject::Type::Array && method == "push" && !args.empty()) {
|
| 1095 |
recv.obj->elements.push_back(args[0]);
|
| 1096 |
ret = JsValue::make_number(static_cast<double>(recv.obj->elements.size()));
|
| 1097 |
+
} else if (recv.obj->props.count("__is_fetch_resp__") && method == "text") {
|
| 1098 |
+
ret = recv.obj->props["body"];
|
| 1099 |
+
} else if (recv.obj->props.count("__is_fetch_resp__") && method == "json") {
|
| 1100 |
+
JsonValue parsed;
|
| 1101 |
+
if (JsonValue::parse(recv.obj->props["body"].to_string(), parsed)) {
|
| 1102 |
+
std::function<JsValue(const JsonValue&)> conv = [&](const JsonValue& jv) -> JsValue {
|
| 1103 |
+
switch (jv.type()) {
|
| 1104 |
+
case JsonValue::Type::Null: return JsValue::make_null();
|
| 1105 |
+
case JsonValue::Type::Bool: return JsValue::make_bool(jv.as_bool());
|
| 1106 |
+
case JsonValue::Type::Number: return JsValue::make_number(jv.as_number());
|
| 1107 |
+
case JsonValue::Type::String: return JsValue::make_string(jv.as_string());
|
| 1108 |
+
case JsonValue::Type::Array: {
|
| 1109 |
+
GcObject* a = alloc_object(GcObject::Type::Array);
|
| 1110 |
+
for (const auto& el : jv.as_array()) a->elements.push_back(conv(el));
|
| 1111 |
+
return JsValue::make_obj(a);
|
| 1112 |
+
}
|
| 1113 |
+
case JsonValue::Type::Object: {
|
| 1114 |
+
GcObject* o = alloc_object(GcObject::Type::Map);
|
| 1115 |
+
for (const auto& [k, v] : jv.as_object()) o->props[k] = conv(v);
|
| 1116 |
+
return JsValue::make_obj(o);
|
| 1117 |
+
}
|
| 1118 |
+
}
|
| 1119 |
+
return JsValue::make_null();
|
| 1120 |
+
};
|
| 1121 |
+
ret = conv(parsed);
|
| 1122 |
+
} else {
|
| 1123 |
+
ret = JsValue::make_null();
|
| 1124 |
+
}
|
| 1125 |
+
} else if (recv.obj->props.count("__is_fetch_resp__") && method == "then" && !args.empty()) {
|
| 1126 |
+
invoke_callable(args[0], {recv}, doc, rep);
|
| 1127 |
+
ret = recv;
|
| 1128 |
} else if (recv.obj->props.count(method) > 0) {
|
| 1129 |
ret = invoke_callable(recv.obj->props[method], args, doc, rep);
|
| 1130 |
}
|
src/js/astra_js.hpp
CHANGED
|
@@ -115,12 +115,23 @@ struct JsExecutionReport {
|
|
| 115 |
GcStats gc;
|
| 116 |
};
|
| 117 |
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
class AstraJsEngine {
|
| 121 |
public:
|
| 122 |
using StorageGetFn = std::function<std::string(const std::string&)>;
|
| 123 |
using StorageSetFn = std::function<void(const std::string&, const std::string&)>;
|
|
|
|
| 124 |
|
| 125 |
AstraJsEngine() = default;
|
| 126 |
~AstraJsEngine();
|
|
@@ -129,9 +140,10 @@ public:
|
|
| 129 |
AstraJsEngine& operator=(const AstraJsEngine&) = delete;
|
| 130 |
|
| 131 |
void bind_storage(StorageGetFn getter, StorageSetFn setter);
|
|
|
|
| 132 |
JsExecutionReport execute(std::string_view code, HtmlDocument& doc);
|
| 133 |
|
| 134 |
-
// Dispatch a real DOM event (e.g. "click") to registered JS bytecode handlers
|
| 135 |
JsExecutionReport dispatch_event(DomNode* target, const std::string& event_name, HtmlDocument& doc);
|
| 136 |
|
| 137 |
// Trigger explicit Mark-and-Sweep Garbage Collection cycle
|
|
@@ -171,9 +183,11 @@ private:
|
|
| 171 |
std::vector<TimerEntry> timers_;
|
| 172 |
// DOM event listeners: (node_ptr, event_name) -> callbacks
|
| 173 |
std::map<std::pair<DomNode*, std::string>, std::vector<JsValue>> listeners_;
|
|
|
|
| 174 |
|
| 175 |
StorageGetFn storage_get_;
|
| 176 |
StorageSetFn storage_set_;
|
|
|
|
| 177 |
};
|
| 178 |
|
| 179 |
} // namespace astra
|
|
|
|
| 115 |
GcStats gc;
|
| 116 |
};
|
| 117 |
|
| 118 |
+
struct JsFetchResult {
|
| 119 |
+
bool ok = false;
|
| 120 |
+
int status = 0;
|
| 121 |
+
std::string body;
|
| 122 |
+
std::string error;
|
| 123 |
+
bool from_cache = false;
|
| 124 |
+
bool blocked_by_shield = false;
|
| 125 |
+
};
|
| 126 |
+
|
| 127 |
+
// AstraJS v3: Lexer -> Recursive-Descent Compiler -> Stack Bytecode VM
|
| 128 |
+
// + Mark-and-Sweep GC + Microtask/Timer Event Loop + DOM Events
|
| 129 |
+
// + Web APIs: fetch() (via AstraShield/HttpCache), JSON.parse/stringify, localStorage.
|
| 130 |
class AstraJsEngine {
|
| 131 |
public:
|
| 132 |
using StorageGetFn = std::function<std::string(const std::string&)>;
|
| 133 |
using StorageSetFn = std::function<void(const std::string&, const std::string&)>;
|
| 134 |
+
using FetchFn = std::function<JsFetchResult(const std::string& url)>;
|
| 135 |
|
| 136 |
AstraJsEngine() = default;
|
| 137 |
~AstraJsEngine();
|
|
|
|
| 140 |
AstraJsEngine& operator=(const AstraJsEngine&) = delete;
|
| 141 |
|
| 142 |
void bind_storage(StorageGetFn getter, StorageSetFn setter);
|
| 143 |
+
void bind_fetch(FetchFn fetcher);
|
| 144 |
JsExecutionReport execute(std::string_view code, HtmlDocument& doc);
|
| 145 |
|
| 146 |
+
// Dispatch a real DOM event (e.g. "click", "input", "submit") to registered JS bytecode handlers
|
| 147 |
JsExecutionReport dispatch_event(DomNode* target, const std::string& event_name, HtmlDocument& doc);
|
| 148 |
|
| 149 |
// Trigger explicit Mark-and-Sweep Garbage Collection cycle
|
|
|
|
| 183 |
std::vector<TimerEntry> timers_;
|
| 184 |
// DOM event listeners: (node_ptr, event_name) -> callbacks
|
| 185 |
std::map<std::pair<DomNode*, std::string>, std::vector<JsValue>> listeners_;
|
| 186 |
+
std::unordered_map<std::string, std::string> local_storage_;
|
| 187 |
|
| 188 |
StorageGetFn storage_get_;
|
| 189 |
StorageSetFn storage_set_;
|
| 190 |
+
FetchFn fetch_fn_;
|
| 191 |
};
|
| 192 |
|
| 193 |
} // namespace astra
|
src/layout/layout_engine.cpp
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
#include "layout/layout_engine.hpp"
|
|
|
|
| 2 |
#include "ipc/ipc_channel.hpp"
|
| 3 |
|
| 4 |
#include <algorithm>
|
|
@@ -10,26 +11,14 @@ namespace astra {
|
|
| 10 |
|
| 11 |
namespace {
|
| 12 |
|
| 13 |
-
int glyph_scale_for_size(float font_size) {
|
| 14 |
-
if (font_size >= 26.0f) return 3;
|
| 15 |
-
if (font_size >= 18.0f) return 2;
|
| 16 |
-
return 1;
|
| 17 |
-
}
|
| 18 |
-
|
| 19 |
-
float char_advance(float font_size, bool bold) {
|
| 20 |
-
int scale = glyph_scale_for_size(font_size);
|
| 21 |
-
return static_cast<float>(scale * 8 + (bold ? 1 : 0));
|
| 22 |
-
}
|
| 23 |
-
|
| 24 |
float line_height_for_size(float font_size) {
|
| 25 |
-
|
| 26 |
-
return static_cast<float>(scale * 8) * 1.65f;
|
| 27 |
}
|
| 28 |
|
| 29 |
-
std::vector<std::string> wrap_text(const std::string& raw, float max_width, float font_size, bool bold) {
|
| 30 |
std::vector<std::string> lines;
|
| 31 |
-
|
| 32 |
-
|
| 33 |
|
| 34 |
std::string cur;
|
| 35 |
size_t i = 0;
|
|
@@ -47,12 +36,14 @@ std::vector<std::string> wrap_text(const std::string& raw, float max_width, floa
|
|
| 47 |
std::string word = raw.substr(wstart, i - wstart);
|
| 48 |
if (cur.empty()) {
|
| 49 |
cur = std::move(word);
|
| 50 |
-
} else if (cur.size() + 1 + word.size() <= max_chars) {
|
| 51 |
-
cur.push_back(' ');
|
| 52 |
-
cur += word;
|
| 53 |
} else {
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
}
|
| 57 |
}
|
| 58 |
if (!cur.empty()) lines.push_back(std::move(cur));
|
|
@@ -143,11 +134,17 @@ void layout_node(LayoutFrame* frame, float x, float y, float avail_w) {
|
|
| 143 |
frame->border_box.h = h + pad_y + st.padding.bottom + st.border_width;
|
| 144 |
return;
|
| 145 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
}
|
| 147 |
|
| 148 |
// Text frame
|
| 149 |
if (frame->node && frame->node->type == NodeType::Text) {
|
| 150 |
-
frame->wrapped_lines = wrap_text(frame->node->text, inner_w, st.font_size, st.bold);
|
| 151 |
float lh = line_height_for_size(st.font_size);
|
| 152 |
frame->content_box.h = static_cast<float>(frame->wrapped_lines.size()) * lh;
|
| 153 |
frame->border_box.h = frame->content_box.h;
|
|
@@ -156,7 +153,41 @@ void layout_node(LayoutFrame* frame, float x, float y, float avail_w) {
|
|
| 156 |
|
| 157 |
float cur_y = frame->content_box.y;
|
| 158 |
|
| 159 |
-
if (st.display == DisplayType::
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
size_t count = frame->children.size();
|
| 161 |
float total_gap = st.gap * static_cast<float>(count > 0 ? count - 1 : 0);
|
| 162 |
float item_w = std::max(40.0f, (inner_w - total_gap) / static_cast<float>(count));
|
|
@@ -243,6 +274,30 @@ void emit_display_items(const LayoutFrame* f, DisplayList& dl) {
|
|
| 243 |
item.text = f->node->get_attr("src");
|
| 244 |
dl.items.push_back(std::move(item));
|
| 245 |
return;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
}
|
| 247 |
}
|
| 248 |
|
|
@@ -256,6 +311,7 @@ void emit_display_items(const LayoutFrame* f, DisplayList& dl) {
|
|
| 256 |
item.color_argb = st.color.to_argb();
|
| 257 |
item.font_size = st.font_size;
|
| 258 |
item.bold = st.bold;
|
|
|
|
| 259 |
item.text = line;
|
| 260 |
dl.items.push_back(std::move(item));
|
| 261 |
ty += lh;
|
|
@@ -299,7 +355,8 @@ std::vector<uint8_t> DisplayList::serialize() const {
|
|
| 299 |
write_f32(buf, it.radius);
|
| 300 |
write_f32(buf, it.thickness);
|
| 301 |
write_f32(buf, it.font_size);
|
| 302 |
-
|
|
|
|
| 303 |
write_str(buf, it.text);
|
| 304 |
}
|
| 305 |
write_u32(buf, static_cast<uint32_t>(hit_regions.size()));
|
|
@@ -328,7 +385,7 @@ bool DisplayList::deserialize(const std::vector<uint8_t>& blob, DisplayList& out
|
|
| 328 |
out.items.reserve(count);
|
| 329 |
for (uint32_t i = 0; i < count; ++i) {
|
| 330 |
DisplayItem it;
|
| 331 |
-
uint32_t t = 0,
|
| 332 |
if (!read_u32(blob, off, t)) return false;
|
| 333 |
it.type = static_cast<DisplayItemType>(t);
|
| 334 |
if (!read_f32(blob, off, it.rect.x)) return false;
|
|
@@ -339,8 +396,10 @@ bool DisplayList::deserialize(const std::vector<uint8_t>& blob, DisplayList& out
|
|
| 339 |
if (!read_f32(blob, off, it.radius)) return false;
|
| 340 |
if (!read_f32(blob, off, it.thickness)) return false;
|
| 341 |
if (!read_f32(blob, off, it.font_size)) return false;
|
| 342 |
-
if (!read_u32(blob, off,
|
| 343 |
-
it.bold = (
|
|
|
|
|
|
|
| 344 |
if (!read_str(blob, off, it.text)) return false;
|
| 345 |
out.items.push_back(std::move(it));
|
| 346 |
}
|
|
|
|
| 1 |
#include "layout/layout_engine.hpp"
|
| 2 |
+
#include "gfx/font_engine.hpp"
|
| 3 |
#include "ipc/ipc_channel.hpp"
|
| 4 |
|
| 5 |
#include <algorithm>
|
|
|
|
| 11 |
|
| 12 |
namespace {
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
float line_height_for_size(float font_size) {
|
| 15 |
+
return FontEngine::instance().line_height(font_size);
|
|
|
|
| 16 |
}
|
| 17 |
|
| 18 |
+
std::vector<std::string> wrap_text(const std::string& raw, float max_width, float font_size, bool bold, bool mono) {
|
| 19 |
std::vector<std::string> lines;
|
| 20 |
+
auto& fe = FontEngine::instance();
|
| 21 |
+
float usable_w = std::max(40.0f, max_width);
|
| 22 |
|
| 23 |
std::string cur;
|
| 24 |
size_t i = 0;
|
|
|
|
| 36 |
std::string word = raw.substr(wstart, i - wstart);
|
| 37 |
if (cur.empty()) {
|
| 38 |
cur = std::move(word);
|
|
|
|
|
|
|
|
|
|
| 39 |
} else {
|
| 40 |
+
std::string cand = cur + " " + word;
|
| 41 |
+
if (fe.measure_text(cand, font_size, bold, mono) <= usable_w) {
|
| 42 |
+
cur = std::move(cand);
|
| 43 |
+
} else {
|
| 44 |
+
lines.push_back(std::move(cur));
|
| 45 |
+
cur = std::move(word);
|
| 46 |
+
}
|
| 47 |
}
|
| 48 |
}
|
| 49 |
if (!cur.empty()) lines.push_back(std::move(cur));
|
|
|
|
| 134 |
frame->border_box.h = h + pad_y + st.padding.bottom + st.border_width;
|
| 135 |
return;
|
| 136 |
}
|
| 137 |
+
if (tag == "input" || tag == "textarea") {
|
| 138 |
+
float h = (st.height > 0.0f) ? st.height : (tag == "textarea" ? 88.0f : 38.0f);
|
| 139 |
+
frame->border_box.h = h;
|
| 140 |
+
frame->content_box.h = std::max(16.0f, h - pad_y - st.padding.bottom - st.border_width);
|
| 141 |
+
return;
|
| 142 |
+
}
|
| 143 |
}
|
| 144 |
|
| 145 |
// Text frame
|
| 146 |
if (frame->node && frame->node->type == NodeType::Text) {
|
| 147 |
+
frame->wrapped_lines = wrap_text(frame->node->text, inner_w, st.font_size, st.bold, st.monospace);
|
| 148 |
float lh = line_height_for_size(st.font_size);
|
| 149 |
frame->content_box.h = static_cast<float>(frame->wrapped_lines.size()) * lh;
|
| 150 |
frame->border_box.h = frame->content_box.h;
|
|
|
|
| 153 |
|
| 154 |
float cur_y = frame->content_box.y;
|
| 155 |
|
| 156 |
+
if (st.display == DisplayType::Grid && !frame->children.empty()) {
|
| 157 |
+
std::vector<GridTrack> cols = st.grid_columns;
|
| 158 |
+
if (cols.empty()) cols = {GridTrack{1.0f, true}, GridTrack{1.0f, true}};
|
| 159 |
+
size_t ncols = cols.size();
|
| 160 |
+
float total_gap = st.gap * static_cast<float>(ncols > 1 ? ncols - 1 : 0);
|
| 161 |
+
float fixed_px = 0.0f;
|
| 162 |
+
float total_fr = 0.0f;
|
| 163 |
+
for (const auto& t : cols) {
|
| 164 |
+
if (t.is_fr) total_fr += t.value;
|
| 165 |
+
else fixed_px += t.value;
|
| 166 |
+
}
|
| 167 |
+
float rem_w = std::max(0.0f, inner_w - total_gap - fixed_px);
|
| 168 |
+
std::vector<float> col_w(ncols, 60.0f);
|
| 169 |
+
std::vector<float> col_x(ncols, frame->content_box.x);
|
| 170 |
+
float cx = frame->content_box.x;
|
| 171 |
+
for (size_t c = 0; c < ncols; ++c) {
|
| 172 |
+
col_w[c] = cols[c].is_fr
|
| 173 |
+
? std::max(40.0f, (total_fr > 0.0f ? rem_w * (cols[c].value / total_fr) : 60.0f))
|
| 174 |
+
: cols[c].value;
|
| 175 |
+
col_x[c] = cx;
|
| 176 |
+
cx += col_w[c] + st.gap;
|
| 177 |
+
}
|
| 178 |
+
size_t idx = 0;
|
| 179 |
+
while (idx < frame->children.size()) {
|
| 180 |
+
float row_h = 0.0f;
|
| 181 |
+
for (size_t c = 0; c < ncols && idx < frame->children.size(); ++c, ++idx) {
|
| 182 |
+
LayoutFrame* ch = frame->children[idx];
|
| 183 |
+
layout_node(ch, col_x[c], cur_y, col_w[c]);
|
| 184 |
+
float ch_h = ch->border_box.h + ch->style.margin.top + ch->style.margin.bottom;
|
| 185 |
+
row_h = std::max(row_h, ch_h);
|
| 186 |
+
}
|
| 187 |
+
cur_y += row_h;
|
| 188 |
+
if (idx < frame->children.size()) cur_y += st.gap;
|
| 189 |
+
}
|
| 190 |
+
} else if (st.display == DisplayType::Flex && st.flex_direction == FlexDirection::Row && !frame->children.empty()) {
|
| 191 |
size_t count = frame->children.size();
|
| 192 |
float total_gap = st.gap * static_cast<float>(count > 0 ? count - 1 : 0);
|
| 193 |
float item_w = std::max(40.0f, (inner_w - total_gap) / static_cast<float>(count));
|
|
|
|
| 274 |
item.text = f->node->get_attr("src");
|
| 275 |
dl.items.push_back(std::move(item));
|
| 276 |
return;
|
| 277 |
+
} else if (tag == "input" || tag == "textarea") {
|
| 278 |
+
std::string id = f->node->id();
|
| 279 |
+
std::string sel = id.empty() ? tag : ("#" + id);
|
| 280 |
+
dl.hit_regions.push_back(HitRegion{HitRegionKind::Input, f->border_box, sel});
|
| 281 |
+
|
| 282 |
+
DisplayItem item;
|
| 283 |
+
item.type = DisplayItemType::DrawInput;
|
| 284 |
+
item.rect = f->border_box;
|
| 285 |
+
item.radius = st.border_radius;
|
| 286 |
+
item.font_size = st.font_size;
|
| 287 |
+
item.focused = (f->node->get_attr("data-astra-focused") == "1");
|
| 288 |
+
std::string val = f->node->get_attr("value");
|
| 289 |
+
std::string ph = f->node->get_attr("placeholder");
|
| 290 |
+
std::string input_type = f->node->get_attr("type");
|
| 291 |
+
if (!val.empty()) {
|
| 292 |
+
if (input_type == "password") item.text.assign(val.size(), '*');
|
| 293 |
+
else item.text = val;
|
| 294 |
+
item.color_argb = st.color.to_argb();
|
| 295 |
+
} else {
|
| 296 |
+
item.text = ph;
|
| 297 |
+
item.color_argb = 0xFF64748Bu; // muted placeholder
|
| 298 |
+
}
|
| 299 |
+
dl.items.push_back(std::move(item));
|
| 300 |
+
return;
|
| 301 |
}
|
| 302 |
}
|
| 303 |
|
|
|
|
| 311 |
item.color_argb = st.color.to_argb();
|
| 312 |
item.font_size = st.font_size;
|
| 313 |
item.bold = st.bold;
|
| 314 |
+
item.monospace = st.monospace;
|
| 315 |
item.text = line;
|
| 316 |
dl.items.push_back(std::move(item));
|
| 317 |
ty += lh;
|
|
|
|
| 355 |
write_f32(buf, it.radius);
|
| 356 |
write_f32(buf, it.thickness);
|
| 357 |
write_f32(buf, it.font_size);
|
| 358 |
+
uint32_t flags = (it.bold ? 1u : 0u) | (it.monospace ? 2u : 0u) | (it.focused ? 4u : 0u);
|
| 359 |
+
write_u32(buf, flags);
|
| 360 |
write_str(buf, it.text);
|
| 361 |
}
|
| 362 |
write_u32(buf, static_cast<uint32_t>(hit_regions.size()));
|
|
|
|
| 385 |
out.items.reserve(count);
|
| 386 |
for (uint32_t i = 0; i < count; ++i) {
|
| 387 |
DisplayItem it;
|
| 388 |
+
uint32_t t = 0, flags = 0;
|
| 389 |
if (!read_u32(blob, off, t)) return false;
|
| 390 |
it.type = static_cast<DisplayItemType>(t);
|
| 391 |
if (!read_f32(blob, off, it.rect.x)) return false;
|
|
|
|
| 396 |
if (!read_f32(blob, off, it.radius)) return false;
|
| 397 |
if (!read_f32(blob, off, it.thickness)) return false;
|
| 398 |
if (!read_f32(blob, off, it.font_size)) return false;
|
| 399 |
+
if (!read_u32(blob, off, flags)) return false;
|
| 400 |
+
it.bold = (flags & 1u) != 0;
|
| 401 |
+
it.monospace = (flags & 2u) != 0;
|
| 402 |
+
it.focused = (flags & 4u) != 0;
|
| 403 |
if (!read_str(blob, off, it.text)) return false;
|
| 404 |
out.items.push_back(std::move(it));
|
| 405 |
}
|
src/layout/layout_engine.hpp
CHANGED
|
@@ -32,7 +32,8 @@ enum class DisplayItemType : uint32_t {
|
|
| 32 |
DrawText = 3,
|
| 33 |
Embed3D = 4,
|
| 34 |
EmbedVideo = 5,
|
| 35 |
-
DrawImage = 6
|
|
|
|
| 36 |
};
|
| 37 |
|
| 38 |
struct DisplayItem {
|
|
@@ -43,6 +44,8 @@ struct DisplayItem {
|
|
| 43 |
float thickness = 1.0f;
|
| 44 |
float font_size = 14.0f;
|
| 45 |
bool bold = false;
|
|
|
|
|
|
|
| 46 |
std::string text;
|
| 47 |
};
|
| 48 |
|
|
@@ -50,13 +53,14 @@ enum class HitRegionKind : uint32_t {
|
|
| 50 |
Link = 1,
|
| 51 |
Button = 2,
|
| 52 |
Scene3D = 3,
|
| 53 |
-
Video = 4
|
|
|
|
| 54 |
};
|
| 55 |
|
| 56 |
struct HitRegion {
|
| 57 |
HitRegionKind kind = HitRegionKind::Link;
|
| 58 |
Rect rect;
|
| 59 |
-
std::string target; // href for Link, id/selector for Button, src for Video/3D
|
| 60 |
};
|
| 61 |
|
| 62 |
struct DisplayList {
|
|
|
|
| 32 |
DrawText = 3,
|
| 33 |
Embed3D = 4,
|
| 34 |
EmbedVideo = 5,
|
| 35 |
+
DrawImage = 6,
|
| 36 |
+
DrawInput = 7
|
| 37 |
};
|
| 38 |
|
| 39 |
struct DisplayItem {
|
|
|
|
| 44 |
float thickness = 1.0f;
|
| 45 |
float font_size = 14.0f;
|
| 46 |
bool bold = false;
|
| 47 |
+
bool monospace = false;
|
| 48 |
+
bool focused = false;
|
| 49 |
std::string text;
|
| 50 |
};
|
| 51 |
|
|
|
|
| 53 |
Link = 1,
|
| 54 |
Button = 2,
|
| 55 |
Scene3D = 3,
|
| 56 |
+
Video = 4,
|
| 57 |
+
Input = 5
|
| 58 |
};
|
| 59 |
|
| 60 |
struct HitRegion {
|
| 61 |
HitRegionKind kind = HitRegionKind::Link;
|
| 62 |
Rect rect;
|
| 63 |
+
std::string target; // href for Link, id/selector for Button/Input, src for Video/3D
|
| 64 |
};
|
| 65 |
|
| 66 |
struct DisplayList {
|
src/main.cpp
CHANGED
|
@@ -5,6 +5,7 @@
|
|
| 5 |
#include "docs/doc_viewer.hpp"
|
| 6 |
#include "ext/extension_runtime.hpp"
|
| 7 |
#include "gfx/astra3d.hpp"
|
|
|
|
| 8 |
#include "gfx/image_decoder.hpp"
|
| 9 |
#include "gfx/rasterizer.hpp"
|
| 10 |
#include "ipc/ipc_channel.hpp"
|
|
@@ -42,7 +43,7 @@ void expect(bool cond, const std::string& label) {
|
|
| 42 |
}
|
| 43 |
|
| 44 |
int run_full_test_suite() {
|
| 45 |
-
std::cout << "=== Astra Browser Core
|
| 46 |
|
| 47 |
// 1. SlabArena + OS Reclamation
|
| 48 |
{
|
|
@@ -233,6 +234,113 @@ int run_full_test_suite() {
|
|
| 233 |
expect(mem.total_rss_bytes > 0 && mem.total_rss_bytes < 256ULL * 1024 * 1024 && mem.processes.size() >= 3,
|
| 234 |
"MemoryGovernor tracks real Linux /proc RSS across Main + Content + RDD workers");
|
| 235 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
app.stop_workers();
|
| 237 |
}
|
| 238 |
|
|
@@ -241,7 +349,7 @@ int run_full_test_suite() {
|
|
| 241 |
}
|
| 242 |
|
| 243 |
int run_stress_test(int tab_count, uint64_t ceiling_mb) {
|
| 244 |
-
std::cout << "=== Astra
|
| 245 |
BrowserApp app(ceiling_mb * 1024ULL * 1024ULL);
|
| 246 |
app.start_workers();
|
| 247 |
|
|
@@ -300,12 +408,13 @@ int run_stress_test(int tab_count, uint64_t ceiling_mb) {
|
|
| 300 |
|
| 301 |
void print_help() {
|
| 302 |
std::cout
|
| 303 |
-
<< "Astra Browser Core
|
| 304 |
<< "Usage:\n"
|
| 305 |
-
<< " astra --test Run full Session
|
| 306 |
<< " astra --url <URL> [--out <file.png>] Load URL & render PNG\n"
|
| 307 |
<< " [--ext <extension_dir>] Load Manifest V3 WebExtension\n"
|
| 308 |
<< " [--scroll <px>] [--click <x,y>] Simulate APZ scroll or mouse click before render\n"
|
|
|
|
| 309 |
<< " [--video-frame <N>] [--wireframe] Select video frame or 3D wireframe mode\n"
|
| 310 |
<< " [--gui] Open interactive Linux X11 desktop window\n"
|
| 311 |
<< " astra --stress-tabs <N> [--cap <MB>] Stress-test N tabs with real /proc RSS check\n"
|
|
@@ -320,7 +429,8 @@ int main(int argc, char** argv) {
|
|
| 320 |
return 0;
|
| 321 |
}
|
| 322 |
|
| 323 |
-
std::string mode_url, out_png, ext_dir
|
|
|
|
| 324 |
bool run_tests = false, open_gui = false, run_repl = false, wireframe = false;
|
| 325 |
int stress_tabs = 0;
|
| 326 |
float scroll_px = 0.0f;
|
|
@@ -336,7 +446,9 @@ int main(int argc, char** argv) {
|
|
| 336 |
else if (arg == "--url" && i + 1 < argc) mode_url = argv[++i];
|
| 337 |
else if (arg == "--out" && i + 1 < argc) out_png = argv[++i];
|
| 338 |
else if (arg == "--ext" && i + 1 < argc) ext_dir = argv[++i];
|
| 339 |
-
else if (arg == "--click" && i + 1 < argc)
|
|
|
|
|
|
|
| 340 |
else if (arg == "--scroll" && i + 1 < argc) scroll_px = std::strtof(argv[++i], nullptr);
|
| 341 |
else if (arg == "--video-frame" && i + 1 < argc) video_frame = static_cast<uint32_t>(std::atoi(argv[++i]));
|
| 342 |
else if (arg == "--stress-tabs" && i + 1 < argc) stress_tabs = std::atoi(argv[++i]);
|
|
@@ -378,14 +490,22 @@ int main(int argc, char** argv) {
|
|
| 378 |
if (scroll_px != 0.0f) {
|
| 379 |
app.scroll_active_tab(scroll_px);
|
| 380 |
}
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 389 |
}
|
| 390 |
}
|
| 391 |
|
|
@@ -415,7 +535,7 @@ int main(int argc, char** argv) {
|
|
| 415 |
}
|
| 416 |
|
| 417 |
if (run_repl) {
|
| 418 |
-
std::cout << "Astra
|
| 419 |
app.new_tab("astra://home");
|
| 420 |
std::string line;
|
| 421 |
while (std::cout << "astra> " && std::getline(std::cin, line)) {
|
|
@@ -444,6 +564,12 @@ int main(int argc, char** argv) {
|
|
| 444 |
int x = 0, y = 0; iss >> x >> y;
|
| 445 |
auto cr = app.click_at(x, y);
|
| 446 |
std::cout << (cr.handled ? (cr.action + ": " + cr.detail + "\n") : "No interactive hit.\n");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 447 |
} else if (cmd == "mem") {
|
| 448 |
auto m = app.memory_snapshot();
|
| 449 |
std::cout << "Total RSS: " << (m.total_rss_bytes / (1024.0 * 1024.0)) << " MB / "
|
|
|
|
| 5 |
#include "docs/doc_viewer.hpp"
|
| 6 |
#include "ext/extension_runtime.hpp"
|
| 7 |
#include "gfx/astra3d.hpp"
|
| 8 |
+
#include "gfx/font_engine.hpp"
|
| 9 |
#include "gfx/image_decoder.hpp"
|
| 10 |
#include "gfx/rasterizer.hpp"
|
| 11 |
#include "ipc/ipc_channel.hpp"
|
|
|
|
| 43 |
}
|
| 44 |
|
| 45 |
int run_full_test_suite() {
|
| 46 |
+
std::cout << "=== Astra Browser Core v3 — Full Subsystem Verification Suite ===\n\n";
|
| 47 |
|
| 48 |
// 1. SlabArena + OS Reclamation
|
| 49 |
{
|
|
|
|
| 234 |
expect(mem.total_rss_bytes > 0 && mem.total_rss_bytes < 256ULL * 1024 * 1024 && mem.processes.size() >= 3,
|
| 235 |
"MemoryGovernor tracks real Linux /proc RSS across Main + Content + RDD workers");
|
| 236 |
|
| 237 |
+
// 12. AstraFont Native TrueType Vector Typography + UTF-8 + Bounded LRU Cache
|
| 238 |
+
{
|
| 239 |
+
auto& fe = FontEngine::instance();
|
| 240 |
+
float w_en = fe.measure_text("Astra Browser", 18.0f, true, false);
|
| 241 |
+
float w_es = fe.measure_text("Navegación rápida — España", 16.0f, false, false);
|
| 242 |
+
float w_mono = fe.measure_text("0xFF38BDF8", 13.0f, false, true);
|
| 243 |
+
expect(fe.freetype_active() && w_en > 80.0f && w_es > 120.0f && w_mono > 50.0f &&
|
| 244 |
+
fe.cached_glyph_count() >= 15 && fe.cache_bytes() <= FontEngine::kMaxCacheBytes,
|
| 245 |
+
"AstraFont loads libfreetype.so.6 via dlopen, renders UTF-8 TrueType glyphs within 4 MiB LRU cap");
|
| 246 |
+
}
|
| 247 |
+
|
| 248 |
+
// 13. AstraImage v2 Native Pure-C++20 Baseline DCT JPEG Decoder
|
| 249 |
+
{
|
| 250 |
+
std::string jpg = "astra/examples/astra_nebula.jpg";
|
| 251 |
+
if (!std::ifstream(jpg)) jpg = "examples/astra_nebula.jpg";
|
| 252 |
+
DecodedImage jimg = ImageDecoder::decode_file(jpg);
|
| 253 |
+
bool non_uniform = false;
|
| 254 |
+
if (jimg.valid && jimg.argb.size() > 100) {
|
| 255 |
+
uint32_t first = jimg.argb[0];
|
| 256 |
+
for (size_t i = 1; i < jimg.argb.size(); i += 97) {
|
| 257 |
+
if (jimg.argb[i] != first) { non_uniform = true; break; }
|
| 258 |
+
}
|
| 259 |
+
}
|
| 260 |
+
expect(jimg.valid && jimg.format == "JPEG" && jimg.width >= 300 && jimg.height >= 180 && non_uniform,
|
| 261 |
+
"AstraImage v2 pure-C++20 Baseline DCT JPEG decoder decodes real YCbCr JPEG images natively");
|
| 262 |
+
}
|
| 263 |
+
|
| 264 |
+
// 14. CSS Grid Multi-Column + Multi-Row Layout Engine
|
| 265 |
+
{
|
| 266 |
+
SlabArena ga;
|
| 267 |
+
HtmlDocument gdoc = HtmlDocument::parse(
|
| 268 |
+
ga,
|
| 269 |
+
"<html><head><style>"
|
| 270 |
+
".g { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }"
|
| 271 |
+
".c { height: 60px; background: #1e293b; }"
|
| 272 |
+
"</style></head><body><div class=\"g\">"
|
| 273 |
+
"<div class=\"c\">1</div><div class=\"c\">2</div><div class=\"c\">3</div>"
|
| 274 |
+
"<div class=\"c\">4</div><div class=\"c\">5</div><div class=\"c\">6</div>"
|
| 275 |
+
"</div></body></html>");
|
| 276 |
+
CssStylesheet gsheet;
|
| 277 |
+
gsheet.parse(gdoc.collect_stylesheets());
|
| 278 |
+
LayoutFrame* groot = LayoutEngine::build_layout(ga, gdoc, gsheet, 960.0f, 600.0f);
|
| 279 |
+
LayoutFrame* grid = (groot && !groot->children.empty()) ? groot->children[0] : nullptr;
|
| 280 |
+
bool ok_grid = grid && grid->children.size() == 6 &&
|
| 281 |
+
grid->children[0]->border_box.x < grid->children[1]->border_box.x &&
|
| 282 |
+
grid->children[1]->border_box.x < grid->children[2]->border_box.x &&
|
| 283 |
+
std::abs(grid->children[0]->border_box.y - grid->children[2]->border_box.y) < 1.0f &&
|
| 284 |
+
grid->children[3]->border_box.y > grid->children[0]->border_box.y + 60.0f;
|
| 285 |
+
expect(ok_grid,
|
| 286 |
+
"CSS Grid (display: grid + repeat(3, 1fr) + gap) computes multi-column/multi-row geometry accurately");
|
| 287 |
+
}
|
| 288 |
+
|
| 289 |
+
// 15. Interactive <input> Focus, Keyboard Typing & AstraJS v3 input/submit Events
|
| 290 |
+
{
|
| 291 |
+
const std::string fpath = "/tmp/astra_input_test.html";
|
| 292 |
+
{
|
| 293 |
+
std::ofstream f(fpath);
|
| 294 |
+
f << "<!doctype html><html><body>"
|
| 295 |
+
<< "<input id=\"q\" placeholder=\"Search...\">"
|
| 296 |
+
<< "<p id=\"out\">idle</p>"
|
| 297 |
+
<< "<script>"
|
| 298 |
+
<< "let inp = document.querySelector(\"#q\");"
|
| 299 |
+
<< "inp.addEventListener(\"input\", function() { document.querySelector(\"#out\").textContent = \"typing:\" + inp.value; });"
|
| 300 |
+
<< "inp.addEventListener(\"submit\", function() { document.querySelector(\"#out\").textContent = \"submitted:\" + inp.value.toUpperCase(); });"
|
| 301 |
+
<< "</script></body></html>";
|
| 302 |
+
}
|
| 303 |
+
app.new_tab("file://" + fpath);
|
| 304 |
+
const Tab* tptr = app.active_tab();
|
| 305 |
+
int hit_x = 80, hit_y = Framebuffer::kChromeHeight + 40;
|
| 306 |
+
if (tptr) {
|
| 307 |
+
for (const auto& hr : tptr->cached_display_list.hit_regions) {
|
| 308 |
+
if (hr.kind == HitRegionKind::Input) {
|
| 309 |
+
hit_x = static_cast<int>(hr.rect.x + 20);
|
| 310 |
+
hit_y = Framebuffer::kChromeHeight + static_cast<int>(hr.rect.y + 16);
|
| 311 |
+
}
|
| 312 |
+
}
|
| 313 |
+
}
|
| 314 |
+
auto cr = app.click_at(hit_x, hit_y);
|
| 315 |
+
bool typed = app.type_text("astra v3");
|
| 316 |
+
std::string mid_txt = app.active_tab()->live_doc->query_selector("#out")->text_content();
|
| 317 |
+
bool submitted = app.press_enter();
|
| 318 |
+
std::string end_txt = app.active_tab()->live_doc->query_selector("#out")->text_content();
|
| 319 |
+
expect(cr.handled && cr.action == "focus_input" && typed && submitted &&
|
| 320 |
+
mid_txt == "typing:astra v3" && end_txt == "submitted:ASTRA V3",
|
| 321 |
+
"Interactive <input> focus, keyboard typing, and Enter dispatch real input/submit events in AstraJS v3");
|
| 322 |
+
}
|
| 323 |
+
|
| 324 |
+
// 16. AstraJS v3 Web APIs: fetch() via AstraShield + HttpCache, JSON.parse/stringify, localStorage
|
| 325 |
+
{
|
| 326 |
+
const std::string apath = "/tmp/astra_webapi_test.html";
|
| 327 |
+
{
|
| 328 |
+
std::ofstream f(apath);
|
| 329 |
+
f << "<!doctype html><html><body><p id=\"res\">init</p><script>"
|
| 330 |
+
<< "localStorage.setItem(\"engine\", \"AstraV3\");"
|
| 331 |
+
<< "let obj = JSON.parse(\"{\\\"ver\\\":3,\\\"tags\\\":[\\\"cpp\\\",\\\"fast\\\"]}\");"
|
| 332 |
+
<< "let ser = JSON.stringify({ ok: true, v: obj.ver });"
|
| 333 |
+
<< "let blocked = fetch(\"https://doubleclick.net/pixel.js\");"
|
| 334 |
+
<< "let live = fetch(\"https://example.com\");"
|
| 335 |
+
<< "document.querySelector(\"#res\").textContent = localStorage.getItem(\"engine\") + \"|\" + obj.tags[1] + \"|\" + blocked.blockedByShield + \"|\" + live.status + \"|\" + ser;"
|
| 336 |
+
<< "</script></body></html>";
|
| 337 |
+
}
|
| 338 |
+
app.new_tab("file://" + apath);
|
| 339 |
+
std::string res_txt = app.active_tab()->live_doc->query_selector("#res")->text_content();
|
| 340 |
+
expect(res_txt.find("AstraV3|fast|true|200|") == 0 && res_txt.find("\"v\":3") != std::string::npos,
|
| 341 |
+
"AstraJS v3 executes fetch() (with AstraShield blocking + live HTTPS), JSON.parse/stringify, and localStorage");
|
| 342 |
+
}
|
| 343 |
+
|
| 344 |
app.stop_workers();
|
| 345 |
}
|
| 346 |
|
|
|
|
| 349 |
}
|
| 350 |
|
| 351 |
int run_stress_test(int tab_count, uint64_t ceiling_mb) {
|
| 352 |
+
std::cout << "=== Astra v3 Multi-Tab Stress Test (" << tab_count << " tabs, ceiling " << ceiling_mb << " MB) ===\n";
|
| 353 |
BrowserApp app(ceiling_mb * 1024ULL * 1024ULL);
|
| 354 |
app.start_workers();
|
| 355 |
|
|
|
|
| 408 |
|
| 409 |
void print_help() {
|
| 410 |
std::cout
|
| 411 |
+
<< "Astra Browser Core v3 (Native C++20 Multi-Process Engine)\n"
|
| 412 |
<< "Usage:\n"
|
| 413 |
+
<< " astra --test Run full Session 3 subsystem suite\n"
|
| 414 |
<< " astra --url <URL> [--out <file.png>] Load URL & render PNG\n"
|
| 415 |
<< " [--ext <extension_dir>] Load Manifest V3 WebExtension\n"
|
| 416 |
<< " [--scroll <px>] [--click <x,y>] Simulate APZ scroll or mouse click before render\n"
|
| 417 |
+
<< " [--type <text>] [--enter] Type into focused <input> and press Enter\n"
|
| 418 |
<< " [--video-frame <N>] [--wireframe] Select video frame or 3D wireframe mode\n"
|
| 419 |
<< " [--gui] Open interactive Linux X11 desktop window\n"
|
| 420 |
<< " astra --stress-tabs <N> [--cap <MB>] Stress-test N tabs with real /proc RSS check\n"
|
|
|
|
| 429 |
return 0;
|
| 430 |
}
|
| 431 |
|
| 432 |
+
std::string mode_url, out_png, ext_dir;
|
| 433 |
+
std::vector<std::pair<std::string, std::string>> cli_actions;
|
| 434 |
bool run_tests = false, open_gui = false, run_repl = false, wireframe = false;
|
| 435 |
int stress_tabs = 0;
|
| 436 |
float scroll_px = 0.0f;
|
|
|
|
| 446 |
else if (arg == "--url" && i + 1 < argc) mode_url = argv[++i];
|
| 447 |
else if (arg == "--out" && i + 1 < argc) out_png = argv[++i];
|
| 448 |
else if (arg == "--ext" && i + 1 < argc) ext_dir = argv[++i];
|
| 449 |
+
else if (arg == "--click" && i + 1 < argc) cli_actions.emplace_back("click", argv[++i]);
|
| 450 |
+
else if (arg == "--type" && i + 1 < argc) cli_actions.emplace_back("type", argv[++i]);
|
| 451 |
+
else if (arg == "--enter") cli_actions.emplace_back("enter", "");
|
| 452 |
else if (arg == "--scroll" && i + 1 < argc) scroll_px = std::strtof(argv[++i], nullptr);
|
| 453 |
else if (arg == "--video-frame" && i + 1 < argc) video_frame = static_cast<uint32_t>(std::atoi(argv[++i]));
|
| 454 |
else if (arg == "--stress-tabs" && i + 1 < argc) stress_tabs = std::atoi(argv[++i]);
|
|
|
|
| 490 |
if (scroll_px != 0.0f) {
|
| 491 |
app.scroll_active_tab(scroll_px);
|
| 492 |
}
|
| 493 |
+
for (const auto& [act, val] : cli_actions) {
|
| 494 |
+
if (act == "click") {
|
| 495 |
+
size_t comma = val.find(',');
|
| 496 |
+
if (comma != std::string::npos) {
|
| 497 |
+
int cx = std::atoi(val.substr(0, comma).c_str());
|
| 498 |
+
int cy = std::atoi(val.substr(comma + 1).c_str());
|
| 499 |
+
auto cr = app.click_at(cx, cy);
|
| 500 |
+
std::cout << "[Astra] Click(" << cx << "," << cy << "): "
|
| 501 |
+
<< (cr.handled ? (cr.action + " -> " + cr.detail) : "no hit") << "\n";
|
| 502 |
+
}
|
| 503 |
+
} else if (act == "type") {
|
| 504 |
+
bool ok = app.type_text(val);
|
| 505 |
+
std::cout << "[Astra] Type(\"" << val << "\"): " << (ok ? "OK" : "no focused input") << "\n";
|
| 506 |
+
} else if (act == "enter") {
|
| 507 |
+
bool ok = app.press_enter();
|
| 508 |
+
std::cout << "[Astra] Enter: " << (ok ? "submitted" : "no focused input") << "\n";
|
| 509 |
}
|
| 510 |
}
|
| 511 |
|
|
|
|
| 535 |
}
|
| 536 |
|
| 537 |
if (run_repl) {
|
| 538 |
+
std::cout << "Astra v3 Interactive Shell (open <url>, tabs, switch <id>, scroll <px>, click <x> <y>, type <text>, enter, mem, shield, cache, render <file.png>, quit)\n";
|
| 539 |
app.new_tab("astra://home");
|
| 540 |
std::string line;
|
| 541 |
while (std::cout << "astra> " && std::getline(std::cin, line)) {
|
|
|
|
| 564 |
int x = 0, y = 0; iss >> x >> y;
|
| 565 |
auto cr = app.click_at(x, y);
|
| 566 |
std::cout << (cr.handled ? (cr.action + ": " + cr.detail + "\n") : "No interactive hit.\n");
|
| 567 |
+
} else if (cmd == "type") {
|
| 568 |
+
std::string txt;
|
| 569 |
+
std::getline(iss >> std::ws, txt);
|
| 570 |
+
std::cout << (app.type_text(txt) ? "Typed.\n" : "No focused input.\n");
|
| 571 |
+
} else if (cmd == "enter") {
|
| 572 |
+
std::cout << (app.press_enter() ? "Submitted.\n" : "No focused input.\n");
|
| 573 |
} else if (cmd == "mem") {
|
| 574 |
auto m = app.memory_snapshot();
|
| 575 |
std::cout << "Total RSS: " << (m.total_rss_bytes / (1024.0 * 1024.0)) << " MB / "
|
src/style/css_engine.cpp
CHANGED
|
@@ -144,6 +144,53 @@ EdgeSizes parse_edges(std::string_view val) {
|
|
| 144 |
return e;
|
| 145 |
}
|
| 146 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
void apply_declaration(ComputedStyle& st, const CssDeclaration& d) {
|
| 148 |
const std::string& p = d.property;
|
| 149 |
const std::string& v = d.value;
|
|
@@ -152,11 +199,14 @@ void apply_declaration(ComputedStyle& st, const CssDeclaration& d) {
|
|
| 152 |
if (p == "display") {
|
| 153 |
if (vl == "none") st.display = DisplayType::None;
|
| 154 |
else if (vl == "flex") st.display = DisplayType::Flex;
|
|
|
|
| 155 |
else if (vl == "inline" || vl == "inline-block") st.display = DisplayType::Inline;
|
| 156 |
else st.display = DisplayType::Block;
|
| 157 |
} else if (p == "flex-direction") {
|
| 158 |
st.flex_direction = (vl == "column") ? FlexDirection::Column : FlexDirection::Row;
|
| 159 |
-
} else if (p == "
|
|
|
|
|
|
|
| 160 |
st.gap = StyleEngine::parse_px(v, 0.0f);
|
| 161 |
} else if (p == "width") {
|
| 162 |
if (vl != "auto" && vl.find('%') == std::string::npos) st.width = StyleEngine::parse_px(v, -1.0f);
|
|
@@ -206,6 +256,9 @@ void apply_declaration(ComputedStyle& st, const CssDeclaration& d) {
|
|
| 206 |
st.font_size = std::clamp(StyleEngine::parse_px(v, st.font_size), 8.0f, 64.0f);
|
| 207 |
} else if (p == "font-weight") {
|
| 208 |
st.bold = (vl == "bold" || vl == "600" || vl == "700" || vl == "800" || vl == "900");
|
|
|
|
|
|
|
|
|
|
| 209 |
}
|
| 210 |
}
|
| 211 |
|
|
@@ -222,6 +275,7 @@ void apply_ua_defaults(const DomNode* node, ComputedStyle& st) {
|
|
| 222 |
st.background = Color{28, 34, 46, 255};
|
| 223 |
st.color = Color{135, 232, 182, 255};
|
| 224 |
st.padding = {2, 5, 2, 5};
|
|
|
|
| 225 |
}
|
| 226 |
} else if (t == "body") {
|
| 227 |
st.background = Color{13, 17, 24, 255};
|
|
@@ -252,6 +306,16 @@ void apply_ua_defaults(const DomNode* node, ComputedStyle& st) {
|
|
| 252 |
st.border_width = 1.0f;
|
| 253 |
st.border_color = Color{45, 56, 76, 255};
|
| 254 |
st.margin = {6, 0, 12, 0};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
} else if (t == "ul" || t == "ol") {
|
| 256 |
st.padding = {0, 0, 0, 20};
|
| 257 |
st.margin = {4, 0, 10, 0};
|
|
@@ -419,6 +483,7 @@ ComputedStyle StyleEngine::compute_style(const DomNode* node,
|
|
| 419 |
st.color = parent_style->color;
|
| 420 |
st.font_size = parent_style->font_size;
|
| 421 |
st.bold = parent_style->bold;
|
|
|
|
| 422 |
st.line_height = parent_style->line_height;
|
| 423 |
}
|
| 424 |
|
|
|
|
| 144 |
return e;
|
| 145 |
}
|
| 146 |
|
| 147 |
+
std::vector<GridTrack> parse_grid_tracks(std::string_view val) {
|
| 148 |
+
std::vector<GridTrack> tracks;
|
| 149 |
+
std::string s = to_lower(trim(val));
|
| 150 |
+
size_t pos = 0;
|
| 151 |
+
while (pos < s.size()) {
|
| 152 |
+
while (pos < s.size() && std::isspace(static_cast<unsigned char>(s[pos]))) ++pos;
|
| 153 |
+
if (pos >= s.size()) break;
|
| 154 |
+
if (s.compare(pos, 7, "repeat(") == 0) {
|
| 155 |
+
size_t rp = s.find(')', pos + 7);
|
| 156 |
+
if (rp == std::string::npos) break;
|
| 157 |
+
std::string inner = s.substr(pos + 7, rp - (pos + 7));
|
| 158 |
+
size_t comma = inner.find(',');
|
| 159 |
+
if (comma != std::string::npos) {
|
| 160 |
+
int count = std::clamp(std::atoi(inner.substr(0, comma).c_str()), 1, 16);
|
| 161 |
+
std::string unit = trim(inner.substr(comma + 1));
|
| 162 |
+
GridTrack tr;
|
| 163 |
+
if (unit.find("fr") != std::string::npos) {
|
| 164 |
+
tr.is_fr = true;
|
| 165 |
+
tr.value = std::max(0.1f, std::strtof(unit.c_str(), nullptr));
|
| 166 |
+
} else {
|
| 167 |
+
tr.is_fr = false;
|
| 168 |
+
tr.value = std::max(24.0f, StyleEngine::parse_px(unit, 120.0f));
|
| 169 |
+
}
|
| 170 |
+
for (int i = 0; i < count; ++i) tracks.push_back(tr);
|
| 171 |
+
}
|
| 172 |
+
pos = rp + 1;
|
| 173 |
+
} else {
|
| 174 |
+
size_t st = pos;
|
| 175 |
+
while (pos < s.size() && !std::isspace(static_cast<unsigned char>(s[pos]))) ++pos;
|
| 176 |
+
std::string tok = s.substr(st, pos - st);
|
| 177 |
+
GridTrack tr;
|
| 178 |
+
if (tok.find("fr") != std::string::npos) {
|
| 179 |
+
tr.is_fr = true;
|
| 180 |
+
tr.value = std::max(0.1f, std::strtof(tok.c_str(), nullptr));
|
| 181 |
+
} else if (tok == "auto") {
|
| 182 |
+
tr.is_fr = true;
|
| 183 |
+
tr.value = 1.0f;
|
| 184 |
+
} else {
|
| 185 |
+
tr.is_fr = false;
|
| 186 |
+
tr.value = std::max(24.0f, StyleEngine::parse_px(tok, 120.0f));
|
| 187 |
+
}
|
| 188 |
+
tracks.push_back(tr);
|
| 189 |
+
}
|
| 190 |
+
}
|
| 191 |
+
return tracks;
|
| 192 |
+
}
|
| 193 |
+
|
| 194 |
void apply_declaration(ComputedStyle& st, const CssDeclaration& d) {
|
| 195 |
const std::string& p = d.property;
|
| 196 |
const std::string& v = d.value;
|
|
|
|
| 199 |
if (p == "display") {
|
| 200 |
if (vl == "none") st.display = DisplayType::None;
|
| 201 |
else if (vl == "flex") st.display = DisplayType::Flex;
|
| 202 |
+
else if (vl == "grid") st.display = DisplayType::Grid;
|
| 203 |
else if (vl == "inline" || vl == "inline-block") st.display = DisplayType::Inline;
|
| 204 |
else st.display = DisplayType::Block;
|
| 205 |
} else if (p == "flex-direction") {
|
| 206 |
st.flex_direction = (vl == "column") ? FlexDirection::Column : FlexDirection::Row;
|
| 207 |
+
} else if (p == "grid-template-columns") {
|
| 208 |
+
st.grid_columns = parse_grid_tracks(v);
|
| 209 |
+
} else if (p == "gap" || p == "grid-gap") {
|
| 210 |
st.gap = StyleEngine::parse_px(v, 0.0f);
|
| 211 |
} else if (p == "width") {
|
| 212 |
if (vl != "auto" && vl.find('%') == std::string::npos) st.width = StyleEngine::parse_px(v, -1.0f);
|
|
|
|
| 256 |
st.font_size = std::clamp(StyleEngine::parse_px(v, st.font_size), 8.0f, 64.0f);
|
| 257 |
} else if (p == "font-weight") {
|
| 258 |
st.bold = (vl == "bold" || vl == "600" || vl == "700" || vl == "800" || vl == "900");
|
| 259 |
+
} else if (p == "font-family") {
|
| 260 |
+
st.monospace = (vl.find("mono") != std::string::npos || vl.find("code") != std::string::npos ||
|
| 261 |
+
vl.find("consolas") != std::string::npos || vl.find("courier") != std::string::npos);
|
| 262 |
}
|
| 263 |
}
|
| 264 |
|
|
|
|
| 275 |
st.background = Color{28, 34, 46, 255};
|
| 276 |
st.color = Color{135, 232, 182, 255};
|
| 277 |
st.padding = {2, 5, 2, 5};
|
| 278 |
+
st.monospace = true;
|
| 279 |
}
|
| 280 |
} else if (t == "body") {
|
| 281 |
st.background = Color{13, 17, 24, 255};
|
|
|
|
| 306 |
st.border_width = 1.0f;
|
| 307 |
st.border_color = Color{45, 56, 76, 255};
|
| 308 |
st.margin = {6, 0, 12, 0};
|
| 309 |
+
st.monospace = true;
|
| 310 |
+
} else if (t == "input" || t == "textarea") {
|
| 311 |
+
st.background = Color{15, 23, 42, 255};
|
| 312 |
+
st.color = Color{241, 245, 249, 255};
|
| 313 |
+
st.border_width = 1.0f;
|
| 314 |
+
st.border_color = Color{51, 65, 85, 255};
|
| 315 |
+
st.border_radius = 6.0f;
|
| 316 |
+
st.padding = {8, 12, 8, 12};
|
| 317 |
+
st.font_size = 14.0f;
|
| 318 |
+
st.height = (t == "textarea") ? 88.0f : 38.0f;
|
| 319 |
} else if (t == "ul" || t == "ol") {
|
| 320 |
st.padding = {0, 0, 0, 20};
|
| 321 |
st.margin = {4, 0, 10, 0};
|
|
|
|
| 483 |
st.color = parent_style->color;
|
| 484 |
st.font_size = parent_style->font_size;
|
| 485 |
st.bold = parent_style->bold;
|
| 486 |
+
st.monospace = parent_style->monospace;
|
| 487 |
st.line_height = parent_style->line_height;
|
| 488 |
}
|
| 489 |
|
src/style/css_engine.hpp
CHANGED
|
@@ -36,6 +36,7 @@ enum class DisplayType : uint8_t {
|
|
| 36 |
Block,
|
| 37 |
Inline,
|
| 38 |
Flex,
|
|
|
|
| 39 |
None
|
| 40 |
};
|
| 41 |
|
|
@@ -44,6 +45,11 @@ enum class FlexDirection : uint8_t {
|
|
| 44 |
Column
|
| 45 |
};
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
struct EdgeSizes {
|
| 48 |
float top = 0;
|
| 49 |
float right = 0;
|
|
@@ -54,6 +60,7 @@ struct EdgeSizes {
|
|
| 54 |
struct ComputedStyle {
|
| 55 |
DisplayType display = DisplayType::Block;
|
| 56 |
FlexDirection flex_direction = FlexDirection::Row;
|
|
|
|
| 57 |
float gap = 0.0f;
|
| 58 |
float width = -1.0f; // < 0 means auto
|
| 59 |
float height = -1.0f; // < 0 means auto
|
|
@@ -66,7 +73,8 @@ struct ComputedStyle {
|
|
| 66 |
Color color{230, 234, 240, 255};
|
| 67 |
float font_size = 14.0f;
|
| 68 |
bool bold = false;
|
| 69 |
-
|
|
|
|
| 70 |
};
|
| 71 |
|
| 72 |
struct CssDeclaration {
|
|
|
|
| 36 |
Block,
|
| 37 |
Inline,
|
| 38 |
Flex,
|
| 39 |
+
Grid,
|
| 40 |
None
|
| 41 |
};
|
| 42 |
|
|
|
|
| 45 |
Column
|
| 46 |
};
|
| 47 |
|
| 48 |
+
struct GridTrack {
|
| 49 |
+
float value = 1.0f;
|
| 50 |
+
bool is_fr = true;
|
| 51 |
+
};
|
| 52 |
+
|
| 53 |
struct EdgeSizes {
|
| 54 |
float top = 0;
|
| 55 |
float right = 0;
|
|
|
|
| 60 |
struct ComputedStyle {
|
| 61 |
DisplayType display = DisplayType::Block;
|
| 62 |
FlexDirection flex_direction = FlexDirection::Row;
|
| 63 |
+
std::vector<GridTrack> grid_columns;
|
| 64 |
float gap = 0.0f;
|
| 65 |
float width = -1.0f; // < 0 means auto
|
| 66 |
float height = -1.0f; // < 0 means auto
|
|
|
|
| 73 |
Color color{230, 234, 240, 255};
|
| 74 |
float font_size = 14.0f;
|
| 75 |
bool bold = false;
|
| 76 |
+
bool monospace = false;
|
| 77 |
+
float line_height = 1.48f;
|
| 78 |
};
|
| 79 |
|
| 80 |
struct CssDeclaration {
|