CSS: le undici proprietà che mancavano, e i selettori :where/:is/:not
Blocco 1, proprietà (quasi tutte in style.rs e paint.rs):
- font-style: corsivo **sintetico** — la mappa di copertura dei glifi viene
inclinata di ~14°, perché con un font per famiglia non c'è la faccia corsiva.
Voce di cache distinta, quindi tondo e corsivo convivono
- text-decoration: sottolineato e barrato, disegnati come linee con spessore
proporzionato al corpo del testo
- text-transform: uppercase/lowercase/capitalize applicate **prima** dello
shaping, perché le maiuscole possono avere avanzamenti diversi
- letter-spacing: aggiunta agli avanzamenti in shape_spaced(), così misura e
disegno restano d'accordo
- opacity: moltiplicata lungo l'albero in build_display_list (50% dentro 50% = 25%)
- border-radius: riempimento a scansione con il cerchio del raggio
- min-height/max-height/min-width applicati dove si decide l'altezza
- overflow: hidden con ritaglio intersecato scendendo l'albero
- background-image: caricata al momento del disegno, non del layout
- linear-gradient: direzioni, angoli in gradi, fermate percentuali o uniformi
Regole UA che mancavano: link blu sottolineati, em/i corsivi, b/strong grassetti,
s/del barrati. E lo sfondo di html/body si propaga alla tela (prima colorava solo
il box: il caso 16 documentava il comportamento sbagliato, l'ho corretto).
Blocco 2, selettori (css.rs, style.rs): :where(), :is(), :not(), :root,
:first-child, :last-child, :empty, :link/:any-link e il combinatore ">".
Il punto non è l'eleganza: i temi WordPress usano :where() 125 volte e :not() 66,
e il parser li scartava — per questo i link restavano blu di default e il tema
non si applicava. Ora il blog dell'utente ha intestazione, titoli e link con lo
stile del proprio tema.
Due difetti di struttura trovati per strada:
- layout_document si porta via la cache immagini (mem::replace) e ne lascia una
finta con base_dir ".": gli sfondi relativi si cercavano nella cartella
sbagliata. Ora il disegno ha la sua cache, con la cartella giusta
- il parser divideva i selettori sulle virgole senza guardare le parentesi,
quindi :is(.a,.b) si spezzava in due e la regola veniva persa
Prove: 112 reftest (12 nuovi: proprietà 101-107, selettori 108-112), 99 unitari.
Un test unitario e il caso 16 codificavano limiti vecchi ("i combinatori non sono
supportati", "il body non è la tela"): aggiornati al comportamento corretto, non
il contrario.
Fuori ambito, dichiarato: bordi con raggio (si arrotondano sfondo e immagini, non
il bordo spesso), background-size/repeat (lo sfondo si stira al box), gradienti
non lineari, :hover/:focus/:nth-child/:has (documento fermo), + e ~ (scartano la
regola invece di approssimarla).
This commit is contained in:
1 parent
ace2be6a43
commit
87b25e771a
22 files changed
+1275
-79
No files matched your search
@@ -1374,6 +1374,7 @@ fn disegna_barra(
|
|||||||
CORPO,
|
CORPO,
|
||||||
bianco,
|
bianco,
|
||||||
400,
|
400,
|
||||||
|
false,
|
||||||
);
|
);
|
||||||
for y in 0..altezza {
|
for y in 0..altezza {
|
||||||
for x in 0..w {
|
for x in 0..w {
|
||||||
|
|||||||
@@ -71,6 +71,8 @@ gusci (Sailfish e desktop) ricostruiti sullo stesso motore.
|
|||||||
**Fuori ambito dichiarato:** multiplexing HTTP/2, `PUSH_PROMISE` e priorità; proxy SOCKS e PAC; invio di file (`input type=file`); sul telefono il servizio di rete resta nel processo, perché un'app sandboxata non avvia processi figli in modo affidabile. **Sul telefono** `fetch` e i cookie sono collegati: sessione di rete nel confine C (`engine_session_*`), documento creato con la sessione, richieste degli script eseguite dal guscio e consegnate al motore |
|
**Fuori ambito dichiarato:** multiplexing HTTP/2, `PUSH_PROMISE` e priorità; proxy SOCKS e PAC; invio di file (`input type=file`); sul telefono il servizio di rete resta nel processo, perché un'app sandboxata non avvia processi figli in modo affidabile. **Sul telefono** `fetch` e i cookie sono collegati: sessione di rete nel confine C (`engine_session_*`), documento creato con la sessione, richieste degli script eseguite dal guscio e consegnate al motore |
|
||||||
| M2.8 Grid | `display:grid`, `grid-template-columns/rows` (`px`, `%`, `fr`, `auto`, `minmax`, `repeat`), `gap`, posizionamento automatico, `grid-column/row: span N`, `align-items` | **fatto** (casi 92–98): elementi con altezza automatica riempiono la riga (`stretch`), righe implicite dall'altezza del contenuto. **Fuori ambito dichiarato:** posizionamento esplicito (`grid-column: 2 / 4`), aree e linee con nome, `grid-auto-flow: column`, `justify-items`, ridistribuzione delle altezze su più righe con `span`, `auto-fill`/`auto-fit` (valgono una traccia) |
|
| M2.8 Grid | `display:grid`, `grid-template-columns/rows` (`px`, `%`, `fr`, `auto`, `minmax`, `repeat`), `gap`, posizionamento automatico, `grid-column/row: span N`, `align-items` | **fatto** (casi 92–98): elementi con altezza automatica riempiono la riga (`stretch`), righe implicite dall'altezza del contenuto. **Fuori ambito dichiarato:** posizionamento esplicito (`grid-column: 2 / 4`), aree e linee con nome, `grid-auto-flow: column`, `justify-items`, ridistribuzione delle altezze su più righe con `span`, `auto-fill`/`auto-fit` (valgono una traccia) |
|
||||||
| M2.9 Immagini | `max-width` applicato alle immagini sostituite, `height:auto` che segue il rapporto (attributi `width`/`height` o proporzione intrinseca), percentuali risolte sul **contenitore** e non sullo spazio disponibile | **fatto** (casi 99–100). Era il difetto che faceva uscire le copertine 1200×630 da una colonna da 700: la regola `img{max-width:100%;height:auto}` di quasi ogni tema veniva ignorata |
|
| M2.9 Immagini | `max-width` applicato alle immagini sostituite, `height:auto` che segue il rapporto (attributi `width`/`height` o proporzione intrinseca), percentuali risolte sul **contenitore** e non sullo spazio disponibile | **fatto** (casi 99–100). Era il difetto che faceva uscire le copertine 1200×630 da una colonna da 700: la regola `img{max-width:100%;height:auto}` di quasi ogni tema veniva ignorata |
|
||||||
|
| M2.10 Proprietà CSS | `font-style` (corsivo sintetico), `text-decoration` (sottolineato e barrato), `text-transform`, `letter-spacing`, `opacity`, `border-radius`, `min/max-height`, `min-width`, `overflow: hidden`, `background-image`, `linear-gradient`; regole UA per link, `em`/`i`, `b`/`strong`, `s`/`del`; sfondo di `html`/`body` propagato alla tela | **fatto** (casi 101–107), con prove a pixel |
|
||||||
|
| M2.11 Selettori | `:where()`, `:is()`, `:not()`, `:root`, `:first-child`, `:last-child`, `:empty`, `:link`/`:any-link`, combinatore `>`; divisione dei selettori e dei composti consapevole delle parentesi | **fatto** (casi 108–112): senza `:where()` metà del CSS dei temi WordPress viene scartato. **Fuori ambito:** `:hover`/`:focus`/`:nth-child()`/`:has()` non corrispondono (documento fermo), `+` e `~` scartano la regola, pseudo-elementi |
|
||||||
| M2.7 Compositor | tile, danni, scroll compositato, animazioni (`wgpu`) | da fare |
|
| M2.7 Compositor | tile, danni, scroll compositato, animazioni (`wgpu`) | da fare |
|
||||||
|
|
||||||
### M2.6, stato preciso
|
### M2.6, stato preciso
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
{
|
||||||
|
"name": "101-min-height",
|
||||||
|
"doc": "altezza 10 ma min-height 40: il box è alto 40.",
|
||||||
|
"html": "<div style=\"width:50px;height:10px;min-height:40px;background:#0000ff\"></div>",
|
||||||
|
"css": "",
|
||||||
|
"viewport": [
|
||||||
|
300,
|
||||||
|
240
|
||||||
|
],
|
||||||
|
"probes": [],
|
||||||
|
"boxes": [
|
||||||
|
{
|
||||||
|
"path": "html>body>div:0",
|
||||||
|
"rect": [
|
||||||
|
8,
|
||||||
|
8,
|
||||||
|
50,
|
||||||
|
40
|
||||||
|
],
|
||||||
|
"tol": 0.8
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
{
|
||||||
|
"name": "102-max-height",
|
||||||
|
"doc": "altezza 80 ma max-height 30: il box è alto 30.",
|
||||||
|
"html": "<div style=\"width:50px;height:80px;max-height:30px;background:#0000ff\"></div>",
|
||||||
|
"css": "",
|
||||||
|
"viewport": [
|
||||||
|
300,
|
||||||
|
240
|
||||||
|
],
|
||||||
|
"probes": [],
|
||||||
|
"boxes": [
|
||||||
|
{
|
||||||
|
"path": "html>body>div:0",
|
||||||
|
"rect": [
|
||||||
|
8,
|
||||||
|
8,
|
||||||
|
50,
|
||||||
|
30
|
||||||
|
],
|
||||||
|
"tol": 0.8
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
{
|
||||||
|
"name": "103-border-radius-angoli",
|
||||||
|
"doc": "rettangolo 100x40 con raggio 20: l'angolo in alto a sinistra resta bianco, il centro è blu.",
|
||||||
|
"html": "<div style=\"width:100px;height:40px;background:#0000ff;border-radius:20px\"></div>",
|
||||||
|
"css": "",
|
||||||
|
"viewport": [
|
||||||
|
300,
|
||||||
|
240
|
||||||
|
],
|
||||||
|
"probes": [
|
||||||
|
{
|
||||||
|
"x": 9,
|
||||||
|
"y": 9,
|
||||||
|
"rgb": [
|
||||||
|
255,
|
||||||
|
255,
|
||||||
|
255
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"x": 58,
|
||||||
|
"y": 28,
|
||||||
|
"rgb": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
255
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"boxes": []
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
{
|
||||||
|
"name": "104-opacity",
|
||||||
|
"doc": "rosso pieno con opacity 0.5 su bianco: (255,128,128).",
|
||||||
|
"html": "<div style=\"width:100px;height:40px;background:#ff0000;opacity:0.5\"></div>",
|
||||||
|
"css": "",
|
||||||
|
"viewport": [
|
||||||
|
300,
|
||||||
|
240
|
||||||
|
],
|
||||||
|
"probes": [
|
||||||
|
{
|
||||||
|
"x": 58,
|
||||||
|
"y": 28,
|
||||||
|
"rgb": [
|
||||||
|
255,
|
||||||
|
128,
|
||||||
|
128
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"boxes": []
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
{
|
||||||
|
"name": "105-gradiente",
|
||||||
|
"doc": "gradiente lineare da rosso a blu da sinistra a destra: al centro (128,0,128).",
|
||||||
|
"html": "<div style=\"width:100px;height:40px;background:linear-gradient(to right, #ff0000, #0000ff)\"></div>",
|
||||||
|
"css": "",
|
||||||
|
"viewport": [
|
||||||
|
300,
|
||||||
|
240
|
||||||
|
],
|
||||||
|
"probes": [
|
||||||
|
{
|
||||||
|
"x": 58,
|
||||||
|
"y": 28,
|
||||||
|
"rgb": [
|
||||||
|
128,
|
||||||
|
0,
|
||||||
|
128
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"boxes": []
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
{
|
||||||
|
"name": "106-sfondo-immagine",
|
||||||
|
"doc": "sfondo da immagine (quadrato blu): il riquadro si riempie di blu.",
|
||||||
|
"html": "<div style=\"width:100px;height:40px;background-image:url(quadrato-blu.png)\"></div>",
|
||||||
|
"css": "",
|
||||||
|
"viewport": [
|
||||||
|
300,
|
||||||
|
240
|
||||||
|
],
|
||||||
|
"probes": [
|
||||||
|
{
|
||||||
|
"x": 58,
|
||||||
|
"y": 28,
|
||||||
|
"rgb": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
255
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"boxes": []
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
{
|
||||||
|
"name": "107-overflow-hidden",
|
||||||
|
"doc": "figlio largo 200 dentro un padre da 40 con overflow:hidden: fuori dal padre resta bianco.",
|
||||||
|
"html": "<div style=\"width:40px;height:40px;overflow:hidden\"><div style=\"width:200px;height:40px;background:#ff9900\"></div></div>",
|
||||||
|
"css": "",
|
||||||
|
"viewport": [
|
||||||
|
300,
|
||||||
|
240
|
||||||
|
],
|
||||||
|
"probes": [
|
||||||
|
{
|
||||||
|
"x": 20,
|
||||||
|
"y": 28,
|
||||||
|
"rgb": [
|
||||||
|
255,
|
||||||
|
153,
|
||||||
|
0
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"x": 60,
|
||||||
|
"y": 28,
|
||||||
|
"rgb": [
|
||||||
|
255,
|
||||||
|
255,
|
||||||
|
255
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"boxes": []
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
{
|
||||||
|
"name": "108-where",
|
||||||
|
"doc": "`:where(.a)` applica lo sfondo: era il selettore che i temi moderni usano ovunque.",
|
||||||
|
"html": "<div class=\"a\" style=\"width:60px;height:20px\"></div>",
|
||||||
|
"css": ":where(.a){background:#ff0000}",
|
||||||
|
"viewport": [
|
||||||
|
300,
|
||||||
|
200
|
||||||
|
],
|
||||||
|
"probes": [
|
||||||
|
{
|
||||||
|
"x": 20,
|
||||||
|
"y": 18,
|
||||||
|
"rgb": [
|
||||||
|
255,
|
||||||
|
0,
|
||||||
|
0
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"boxes": []
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
{
|
||||||
|
"name": "109-is-e-not",
|
||||||
|
"doc": "`:is()` su due classi e `:not()` che esclude: il primo è rosso, il secondo verde.",
|
||||||
|
"html": "<div class=\"a\" style=\"width:60px;height:20px\"></div><div class=\"a escluso\" style=\"width:60px;height:20px\"></div>",
|
||||||
|
"css": ":is(.a,.b){background:#ff0000}\n:is(.a):not(.escluso){background:#00aa00}",
|
||||||
|
"viewport": [
|
||||||
|
300,
|
||||||
|
200
|
||||||
|
],
|
||||||
|
"probes": [
|
||||||
|
{
|
||||||
|
"x": 20,
|
||||||
|
"y": 18,
|
||||||
|
"rgb": [
|
||||||
|
0,
|
||||||
|
170,
|
||||||
|
0
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"x": 20,
|
||||||
|
"y": 46,
|
||||||
|
"rgb": [
|
||||||
|
255,
|
||||||
|
0,
|
||||||
|
0
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"boxes": []
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
{
|
||||||
|
"name": "110-figlio-diretto",
|
||||||
|
"doc": "`div > p` colpisce il figlio diretto ma non il nipote.",
|
||||||
|
"html": "<div style=\"width:100px\"><p style=\"margin:0;height:10px\"></p><section><p style=\"margin:0;height:10px\"></p></section></div>",
|
||||||
|
"css": "div > p{background:#0000ff}",
|
||||||
|
"viewport": [
|
||||||
|
300,
|
||||||
|
200
|
||||||
|
],
|
||||||
|
"probes": [
|
||||||
|
{
|
||||||
|
"x": 20,
|
||||||
|
"y": 15,
|
||||||
|
"rgb": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
255
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"x": 20,
|
||||||
|
"y": 30,
|
||||||
|
"rgb": [
|
||||||
|
255,
|
||||||
|
255,
|
||||||
|
255
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"boxes": []
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
{
|
||||||
|
"name": "111-first-child",
|
||||||
|
"doc": "`:first-child` colpisce solo il primo elemento.",
|
||||||
|
"html": "<div style=\"width:100px\"><p style=\"margin:0;height:10px\"></p><p style=\"margin:0;height:10px\"></p></div>",
|
||||||
|
"css": "p:first-child{background:#ff0000}",
|
||||||
|
"viewport": [
|
||||||
|
300,
|
||||||
|
200
|
||||||
|
],
|
||||||
|
"probes": [
|
||||||
|
{
|
||||||
|
"x": 20,
|
||||||
|
"y": 15,
|
||||||
|
"rgb": [
|
||||||
|
255,
|
||||||
|
0,
|
||||||
|
0
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"x": 20,
|
||||||
|
"y": 25,
|
||||||
|
"rgb": [
|
||||||
|
255,
|
||||||
|
255,
|
||||||
|
255
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"boxes": []
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
{
|
||||||
|
"name": "112-root",
|
||||||
|
"doc": "`:root` (l'elemento html) imposta lo sfondo della pagina.",
|
||||||
|
"html": "<div style=\"width:20px;height:10px\"></div>",
|
||||||
|
"css": ":root{background:#0000ff}",
|
||||||
|
"viewport": [
|
||||||
|
300,
|
||||||
|
200
|
||||||
|
],
|
||||||
|
"probes": [
|
||||||
|
{
|
||||||
|
"x": 200,
|
||||||
|
"y": 150,
|
||||||
|
"rgb": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
255
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"boxes": []
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "16-sfondo-body",
|
"name": "16-sfondo-body",
|
||||||
"doc": "il body è un box con margine, non il canvas: fuori resta bianco.",
|
"doc": "lo sfondo del body si propaga alla tela, come nei browser: fuori dal box resta #f0f0f0, non bianco.",
|
||||||
"html": "<div style=\"height:10px\"></div>",
|
"html": "<div style=\"height:10px\"></div>",
|
||||||
"css": "body{background:#f0f0f0}",
|
"css": "body{background:#f0f0f0}",
|
||||||
"viewport": [
|
"viewport": [
|
||||||
@@ -12,9 +12,9 @@
|
|||||||
"x": 4,
|
"x": 4,
|
||||||
"y": 4,
|
"y": 4,
|
||||||
"rgb": [
|
"rgb": [
|
||||||
255,
|
240,
|
||||||
255,
|
240,
|
||||||
255
|
240
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -30,9 +30,9 @@
|
|||||||
"x": 195,
|
"x": 195,
|
||||||
"y": 9,
|
"y": 9,
|
||||||
"rgb": [
|
"rgb": [
|
||||||
255,
|
240,
|
||||||
255,
|
240,
|
||||||
255
|
240
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
|||||||
+211
-19
@@ -1,17 +1,42 @@
|
|||||||
//! Parser CSS: regole di stile con selettori composti, discendenza, specificità.
|
//! Parser CSS: regole di stile con selettori composti, discendenza, specificità.
|
||||||
//! Fuori ambito M0: at-rule con condizioni reali, pseudo-classi, combinatore `>`.
|
//! Fuori ambito M0: at-rule con condizioni reali, pseudo-classi, combinatore `>`.
|
||||||
|
|
||||||
|
/// Pseudo-classi riconosciute. Le altre (`:hover`, `:focus`, `:has`, ...) non
|
||||||
|
/// corrispondono mai in un render statico: la regola resta nel foglio e
|
||||||
|
/// semplicemente non si applica, invece di far scartare tutto il selettore.
|
||||||
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
|
pub enum Pseudo {
|
||||||
|
/// `:root`, l'elemento html
|
||||||
|
Root,
|
||||||
|
FirstChild,
|
||||||
|
LastChild,
|
||||||
|
Empty,
|
||||||
|
/// `<a href>` e `<area href>`
|
||||||
|
Link,
|
||||||
|
/// `:not(...)`
|
||||||
|
Not(Vec<Selector>),
|
||||||
|
/// `:is(...)` e `:where(...)`: `azzerata` è vero per `:where`, che non
|
||||||
|
/// contribuisce alla specificità
|
||||||
|
Is(Vec<Selector>, bool),
|
||||||
|
/// pseudo-classe che non si applica a un documento fermo
|
||||||
|
MaiAttiva,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub struct Compound {
|
pub struct Compound {
|
||||||
pub tag: Option<String>,
|
pub tag: Option<String>,
|
||||||
pub id: Option<String>,
|
pub id: Option<String>,
|
||||||
pub classes: Vec<String>,
|
pub classes: Vec<String>,
|
||||||
|
pub pseudo: Vec<Pseudo>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub struct Selector {
|
pub struct Selector {
|
||||||
/// Dal più lontano al soggetto: parti[0] è l'antenato, l'ultima è il soggetto.
|
/// Dal più lontano al soggetto: parti[0] è l'antenato, l'ultima è il soggetto.
|
||||||
pub compounds: Vec<Compound>,
|
pub compounds: Vec<Compound>,
|
||||||
|
/// Relazione con il composto precedente: ' ' discendenza, '>' figlio diretto.
|
||||||
|
/// `combinatori[0]` non si usa.
|
||||||
|
pub combinatori: Vec<char>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
@@ -42,6 +67,27 @@ impl Selector {
|
|||||||
if comp.tag.is_some() {
|
if comp.tag.is_some() {
|
||||||
c += 1;
|
c += 1;
|
||||||
}
|
}
|
||||||
|
for p in &comp.pseudo {
|
||||||
|
match p {
|
||||||
|
// `:where()` non pesa nulla, `:not()` e `:is()` valgono il
|
||||||
|
// selettore più specifico che contengono
|
||||||
|
Pseudo::Is(interni, azzerata) => {
|
||||||
|
if !azzerata {
|
||||||
|
let (ia, ib, ic) = massima_specificita(interni);
|
||||||
|
a += ia;
|
||||||
|
b += ib;
|
||||||
|
c += ic;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Pseudo::Not(interni) => {
|
||||||
|
let (ia, ib, ic) = massima_specificita(interni);
|
||||||
|
a += ia;
|
||||||
|
b += ib;
|
||||||
|
c += ic;
|
||||||
|
}
|
||||||
|
_ => b += 1,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
(a, b, c)
|
(a, b, c)
|
||||||
}
|
}
|
||||||
@@ -137,10 +183,10 @@ fn parse_into(src: &str, order: &mut u32, viewport_w: f32, rules: &mut Vec<Rule>
|
|||||||
if decls.is_empty() {
|
if decls.is_empty() {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// `:root` è l'elemento html e non ha bisogno di pseudo-classi in M1
|
// la divisione sulle virgole deve rispettare le parentesi: `:is(.a,.b)`
|
||||||
let sel_text = sel_text.replace(":root", "html");
|
// è un selettore solo, non due
|
||||||
for sel in sel_text.split(',') {
|
for sel in split_top_level(sel_text.trim(), ',') {
|
||||||
if let Some(selector) = parse_selector(sel) {
|
if let Some(selector) = parse_selector(&sel) {
|
||||||
rules.push(Rule {
|
rules.push(Rule {
|
||||||
selector,
|
selector,
|
||||||
decls: decls.clone(),
|
decls: decls.clone(),
|
||||||
@@ -290,25 +336,79 @@ pub fn parse_declarations(src: &str) -> Vec<Declaration> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_selector(src: &str) -> Option<Selector> {
|
pub fn parse_selector(src: &str) -> Option<Selector> {
|
||||||
let mut compounds = Vec::new();
|
// La divisione deve rispettare le parentesi: `:where(a, b)` contiene uno
|
||||||
for part in src.split_whitespace() {
|
// spazio che non separa composti.
|
||||||
if part.is_empty() || part == ">" || part == "+" || part == "~" {
|
let mut pezzi: Vec<String> = Vec::new();
|
||||||
// combinatori non di discendenza: non supportati in M0
|
let mut corrente = String::new();
|
||||||
if part == ">" || part == "+" || part == "~" {
|
let mut profondita = 0i32;
|
||||||
|
let mut caratteri = src.chars().peekable();
|
||||||
|
while let Some(c) = caratteri.next() {
|
||||||
|
match c {
|
||||||
|
'(' => {
|
||||||
|
profondita += 1;
|
||||||
|
corrente.push(c);
|
||||||
|
}
|
||||||
|
')' => {
|
||||||
|
profondita -= 1;
|
||||||
|
corrente.push(c);
|
||||||
|
}
|
||||||
|
c if profondita == 0 && (c.is_whitespace() || c == '>' || c == '+' || c == '~') => {
|
||||||
|
if !corrente.is_empty() {
|
||||||
|
pezzi.push(std::mem::take(&mut corrente));
|
||||||
|
}
|
||||||
|
if c == '>' || c == '+' || c == '~' {
|
||||||
|
// `+` e `~` restano fuori ambito: la regola si scarta
|
||||||
|
if c != '>' {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
pezzi.push(c.to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c => corrente.push(c),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !corrente.is_empty() {
|
||||||
|
pezzi.push(corrente);
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut compounds: Vec<Compound> = Vec::new();
|
||||||
|
let mut combinatori: Vec<char> = Vec::new();
|
||||||
|
let mut prossimo = ' ';
|
||||||
|
for pezzo in pezzi {
|
||||||
|
if pezzo == ">" {
|
||||||
|
prossimo = '>';
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if let Some(c) = parse_compound(part) {
|
let c = parse_compound(&pezzo)?;
|
||||||
compounds.push(c);
|
compounds.push(c);
|
||||||
} else {
|
combinatori.push(prossimo);
|
||||||
return None;
|
prossimo = ' ';
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if compounds.is_empty() {
|
if compounds.is_empty() {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
Some(Selector { compounds })
|
Some(Selector {
|
||||||
|
compounds,
|
||||||
|
combinatori,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Elenco di selettori separati da virgola, al primo livello di parentesi:
|
||||||
|
/// serve a `:is()`, `:where()` e `:not()`.
|
||||||
|
fn parse_selector_list(src: &str) -> Option<Vec<Selector>> {
|
||||||
|
let mut out = Vec::new();
|
||||||
|
for pezzo in split_top_level(src, ',') {
|
||||||
|
let s = pezzo.trim();
|
||||||
|
if s.is_empty() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
out.push(parse_selector(s)?);
|
||||||
|
}
|
||||||
|
if out.is_empty() {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(out)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -318,6 +418,7 @@ fn parse_compound(src: &str) -> Option<Compound> {
|
|||||||
let mut tag: Option<String> = None;
|
let mut tag: Option<String> = None;
|
||||||
let mut id: Option<String> = None;
|
let mut id: Option<String> = None;
|
||||||
let mut classes: Vec<String> = Vec::new();
|
let mut classes: Vec<String> = Vec::new();
|
||||||
|
let mut pseudo_classi: Vec<Pseudo> = Vec::new();
|
||||||
let mut seen_any = false;
|
let mut seen_any = false;
|
||||||
|
|
||||||
while i < chars.len() {
|
while i < chars.len() {
|
||||||
@@ -346,7 +447,63 @@ fn parse_compound(src: &str) -> Option<Compound> {
|
|||||||
}
|
}
|
||||||
seen_any = true;
|
seen_any = true;
|
||||||
}
|
}
|
||||||
':' => return None, // pseudo-classi fuori ambito M0
|
':' => {
|
||||||
|
// `::before` e simili restano fuori ambito: non si disegnano
|
||||||
|
if i + 1 < chars.len() && chars[i + 1] == ':' {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
i += 1;
|
||||||
|
let inizio = i;
|
||||||
|
while i < chars.len() && (chars[i].is_alphanumeric() || chars[i] == '-') {
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
let nome: String = chars[inizio..i]
|
||||||
|
.iter()
|
||||||
|
.collect::<String>()
|
||||||
|
.to_ascii_lowercase();
|
||||||
|
if nome.is_empty() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let argomento = if i < chars.len() && chars[i] == '(' {
|
||||||
|
let inizio_arg = i + 1;
|
||||||
|
let mut profondita = 1i32;
|
||||||
|
let mut j = inizio_arg;
|
||||||
|
while j < chars.len() && profondita > 0 {
|
||||||
|
match chars[j] {
|
||||||
|
'(' => profondita += 1,
|
||||||
|
')' => profondita -= 1,
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
j += 1;
|
||||||
|
}
|
||||||
|
if profondita != 0 {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let testo: String = chars[inizio_arg..j - 1].iter().collect();
|
||||||
|
i = j;
|
||||||
|
Some(testo)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
let pseudo = match (nome.as_str(), argomento) {
|
||||||
|
("root", None) => Pseudo::Root,
|
||||||
|
("first-child", None) => Pseudo::FirstChild,
|
||||||
|
("last-child", None) => Pseudo::LastChild,
|
||||||
|
("empty", None) => Pseudo::Empty,
|
||||||
|
("link", None) | ("any-link", None) => Pseudo::Link,
|
||||||
|
("not", Some(elenco)) => {
|
||||||
|
let interni = parse_selector_list(&elenco)?;
|
||||||
|
Pseudo::Not(interni)
|
||||||
|
}
|
||||||
|
("is", Some(elenco)) => Pseudo::Is(parse_selector_list(&elenco)?, false),
|
||||||
|
("where", Some(elenco)) => Pseudo::Is(parse_selector_list(&elenco)?, true),
|
||||||
|
// `:has()`, `:hover`, `:focus`, `:nth-child(...)`: non si
|
||||||
|
// applicano a un documento fermo o non sono implementate
|
||||||
|
_ => Pseudo::MaiAttiva,
|
||||||
|
};
|
||||||
|
pseudo_classi.push(pseudo);
|
||||||
|
seen_any = true;
|
||||||
|
}
|
||||||
c if c.is_alphabetic() => {
|
c if c.is_alphabetic() => {
|
||||||
let start = i;
|
let start = i;
|
||||||
while i < chars.len() && (chars[i].is_alphanumeric() || chars[i] == '-') {
|
while i < chars.len() && (chars[i].is_alphanumeric() || chars[i] == '-') {
|
||||||
@@ -366,7 +523,12 @@ fn parse_compound(src: &str) -> Option<Compound> {
|
|||||||
if !seen_any {
|
if !seen_any {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
Some(Compound { tag, id, classes })
|
Some(Compound {
|
||||||
|
tag,
|
||||||
|
id,
|
||||||
|
classes,
|
||||||
|
pseudo: pseudo_classi,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn strip_comments(src: &str) -> String {
|
fn strip_comments(src: &str) -> String {
|
||||||
@@ -482,9 +644,39 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn combinatori_non_supportati_scartano_la_regola() {
|
fn combinatori_e_pseudo() {
|
||||||
assert!(parse_selector("div > p").is_none());
|
// `>` ora è supportato: la regola vale per il figlio diretto
|
||||||
|
let figlio = parse_selector("div > p").expect("figlio diretto");
|
||||||
|
assert_eq!(figlio.combinatori, vec![' ', '>']);
|
||||||
|
// discendenza semplice
|
||||||
assert!(parse_selector("div p").is_some());
|
assert!(parse_selector("div p").is_some());
|
||||||
assert!(parse_selector("a:hover").is_none());
|
// `+` e `~` restano fuori ambito e scartano la regola, invece di
|
||||||
|
// applicarla come discendenza (che sarebbe un falso)
|
||||||
|
assert!(parse_selector("a + b").is_none());
|
||||||
|
assert!(parse_selector("a ~ b").is_none());
|
||||||
|
// `:hover` non corrisponde mai, ma il selettore si analizza
|
||||||
|
|
||||||
|
let sel = parse_selector("a:hover").expect("pseudo riconosciuta");
|
||||||
|
assert!(matches!(
|
||||||
|
sel.compounds[0].pseudo.first(),
|
||||||
|
Some(Pseudo::MaiAttiva)
|
||||||
|
));
|
||||||
|
// `:where()` e `:not()` con elenchi: le virgole non spezzano il selettore
|
||||||
|
let dove = parse_selector(":where(.a, .b)").expect("where");
|
||||||
|
assert_eq!(dove.compounds.len(), 1);
|
||||||
|
let non = parse_selector("div:not(.x, .y)").expect("not");
|
||||||
|
assert_eq!(non.compounds.len(), 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Specificità del selettore più specifico di un elenco (per `:is()`/`:not()`).
|
||||||
|
fn massima_specificita(elenco: &[Selector]) -> (u16, u16, u16) {
|
||||||
|
let mut migliore = (0, 0, 0);
|
||||||
|
for sel in elenco {
|
||||||
|
let s = sel.specificity();
|
||||||
|
if s > migliore {
|
||||||
|
migliore = s;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
migliore
|
||||||
|
}
|
||||||
+91
-11
@@ -47,7 +47,7 @@ thread_local! {
|
|||||||
/// Cache condivisa da tutte le istanze nel processo: un cambio di zoom ripete
|
/// Cache condivisa da tutte le istanze nel processo: un cambio di zoom ripete
|
||||||
/// le stesse lettere alle stesse dimensioni, e senza condivisione ogni render
|
/// le stesse lettere alle stesse dimensioni, e senza condivisione ogni render
|
||||||
/// ripartirebbe da zero rasterizzando le curve.
|
/// ripartirebbe da zero rasterizzando le curve.
|
||||||
static GLYPH_CACHE: RefCell<HashMap<(u64, u16, u32, bool), Rc<GlyphBitmap>>> =
|
static GLYPH_CACHE: RefCell<HashMap<(u64, u16, u32, bool, bool), Rc<GlyphBitmap>>> =
|
||||||
RefCell::new(HashMap::new());
|
RefCell::new(HashMap::new());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -119,6 +119,20 @@ impl FontHolder {
|
|||||||
self.shape_dir(text, px, false)
|
self.shape_dir(text, px, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Shaping con `letter-spacing`: la spaziatura si aggiunge agli avanzamenti,
|
||||||
|
/// così misura e disegno restano d'accordo (il CSS la conta dopo ogni glifo).
|
||||||
|
pub fn shape_spaced(&self, text: &str, px: f32, rtl: bool, spaziatura: f32) -> ShapedText {
|
||||||
|
let mut shaped = self.shape_dir(text, px, rtl);
|
||||||
|
if spaziatura != 0.0 && !shaped.glyphs.is_empty() {
|
||||||
|
for g in &mut shaped.glyphs {
|
||||||
|
g.advance += spaziatura;
|
||||||
|
}
|
||||||
|
shaped.width += spaziatura * shaped.glyphs.len() as f32;
|
||||||
|
shaped.width = shaped.width.max(0.0);
|
||||||
|
}
|
||||||
|
shaped
|
||||||
|
}
|
||||||
|
|
||||||
/// Shaping con direzione: `rtl` inverte l'ordine dei glifi (testo ebraico,
|
/// Shaping con direzione: `rtl` inverte l'ordine dei glifi (testo ebraico,
|
||||||
/// arabo). I glifi tornano in ordine visivo, quindi si disegnano da sinistra.
|
/// arabo). I glifi tornano in ordine visivo, quindi si disegnano da sinistra.
|
||||||
pub fn shape_dir(&self, text: &str, px: f32, rtl: bool) -> ShapedText {
|
pub fn shape_dir(&self, text: &str, px: f32, rtl: bool) -> ShapedText {
|
||||||
@@ -173,6 +187,7 @@ impl FontHolder {
|
|||||||
/// `px` quella del dispositivo: gli avanzamenti vanno riscalati, altrimenti
|
/// `px` quella del dispositivo: gli avanzamenti vanno riscalati, altrimenti
|
||||||
/// a zoom > 1 i glifi crescono ma la penna avanza come a zoom 1 e le lettere
|
/// a zoom > 1 i glifi crescono ma la penna avanza come a zoom 1 e le lettere
|
||||||
/// si sovrappongono.
|
/// si sovrappongono.
|
||||||
|
#[allow(clippy::too_many_arguments)]
|
||||||
pub fn draw_shaped(
|
pub fn draw_shaped(
|
||||||
&self,
|
&self,
|
||||||
buf: &mut [u8],
|
buf: &mut [u8],
|
||||||
@@ -185,9 +200,10 @@ impl FontHolder {
|
|||||||
px: f32,
|
px: f32,
|
||||||
color: Color,
|
color: Color,
|
||||||
weight: u16,
|
weight: u16,
|
||||||
|
italic: bool,
|
||||||
) {
|
) {
|
||||||
self.draw_shaped_in(
|
self.draw_shaped_in(
|
||||||
buf, width, height, x, baseline, shaped, size_css, px, color, weight, None,
|
buf, width, height, x, baseline, shaped, size_css, px, color, weight, italic, None,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -207,6 +223,7 @@ impl FontHolder {
|
|||||||
px: f32,
|
px: f32,
|
||||||
color: Color,
|
color: Color,
|
||||||
weight: u16,
|
weight: u16,
|
||||||
|
italic: bool,
|
||||||
clip: Option<(f32, f32)>,
|
clip: Option<(f32, f32)>,
|
||||||
) {
|
) {
|
||||||
const INV255: f32 = 1.0 / 255.0;
|
const INV255: f32 = 1.0 / 255.0;
|
||||||
@@ -221,7 +238,7 @@ impl FontHolder {
|
|||||||
for &smear in smears {
|
for &smear in smears {
|
||||||
let mut pen = x + smear;
|
let mut pen = x + smear;
|
||||||
for g in &shaped.glyphs {
|
for g in &shaped.glyphs {
|
||||||
let bmp = self.glyph_bitmap(g.id, px, bold);
|
let bmp = self.glyph_bitmap(g.id, px, bold, italic);
|
||||||
let mut fuori = false;
|
let mut fuori = false;
|
||||||
if let Some((lo, hi)) = clip {
|
if let Some((lo, hi)) = clip {
|
||||||
let glifo = pen + g.x_offset * k;
|
let glifo = pen + g.x_offset * k;
|
||||||
@@ -261,18 +278,18 @@ impl FontHolder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn glyph_bitmap(&self, id: u16, px: f32, bold: bool) -> Rc<GlyphBitmap> {
|
fn glyph_bitmap(&self, id: u16, px: f32, bold: bool, italic: bool) -> Rc<GlyphBitmap> {
|
||||||
let size = quantize(px);
|
let size = quantize(px);
|
||||||
let key = (self.hash, id, (size * 2.0) as u32, bold);
|
let key = (self.hash, id, (size * 2.0) as u32, bold, italic);
|
||||||
if let Some(b) = GLYPH_CACHE.with(|c| c.borrow().get(&key).cloned()) {
|
if let Some(b) = GLYPH_CACHE.with(|c| c.borrow().get(&key).cloned()) {
|
||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
let bitmap = Rc::new(self.rasterize(id, size, bold));
|
let bitmap = Rc::new(self.rasterize(id, size, bold, italic));
|
||||||
GLYPH_CACHE.with(|c| c.borrow_mut().insert(key, Rc::clone(&bitmap)));
|
GLYPH_CACHE.with(|c| c.borrow_mut().insert(key, Rc::clone(&bitmap)));
|
||||||
bitmap
|
bitmap
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rasterize(&self, id: u16, size: f32, bold: bool) -> GlyphBitmap {
|
fn rasterize(&self, id: u16, size: f32, bold: bool, italic: bool) -> GlyphBitmap {
|
||||||
// grassetto sintetico: seconda passata a +0,6 px disegnata sopra
|
// grassetto sintetico: seconda passata a +0,6 px disegnata sopra
|
||||||
let smear = if bold { 1 } else { 0 };
|
let smear = if bold { 1 } else { 0 };
|
||||||
let glyph = ab_glyph::Glyph {
|
let glyph = ab_glyph::Glyph {
|
||||||
@@ -316,13 +333,49 @@ impl FontHolder {
|
|||||||
coverage[idx] = v;
|
coverage[idx] = v;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
GlyphBitmap {
|
let mut bitmap = GlyphBitmap {
|
||||||
left,
|
left,
|
||||||
top,
|
top,
|
||||||
w: w as u32,
|
w: w as u32,
|
||||||
h: h as u32,
|
h: h as u32,
|
||||||
coverage,
|
coverage,
|
||||||
|
};
|
||||||
|
if italic {
|
||||||
|
bitmap = inclina(bitmap);
|
||||||
}
|
}
|
||||||
|
bitmap
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Corsivo sintetico: inclina la mappa di copertura di circa 14°, come fanno i
|
||||||
|
/// programmi di impaginazione quando la famiglia non ha una faccia inclinata.
|
||||||
|
/// Le righe in alto si spostano a destra, quelle in basso restano ferme.
|
||||||
|
fn inclina(bitmap: GlyphBitmap) -> GlyphBitmap {
|
||||||
|
const PENDENZA: f32 = 0.25;
|
||||||
|
let h = bitmap.h as usize;
|
||||||
|
let w = bitmap.w as usize;
|
||||||
|
if h == 0 || w == 0 {
|
||||||
|
return bitmap;
|
||||||
|
}
|
||||||
|
let scarto = (PENDENZA * h as f32).ceil() as usize;
|
||||||
|
let nuova_w = w + scarto;
|
||||||
|
let mut copertura = vec![0u8; nuova_w * h];
|
||||||
|
for y in 0..h {
|
||||||
|
// y conta dall'alto: il massimo spostamento è in cima
|
||||||
|
let offset = scarto * (h - 1 - y) / (h - 1).max(1);
|
||||||
|
for x in 0..w {
|
||||||
|
let v = bitmap.coverage[y * w + x];
|
||||||
|
if v != 0 {
|
||||||
|
copertura[y * nuova_w + x + offset] = v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
GlyphBitmap {
|
||||||
|
left: bitmap.left,
|
||||||
|
top: bitmap.top,
|
||||||
|
w: nuova_w as u32,
|
||||||
|
h: bitmap.h,
|
||||||
|
coverage: copertura,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -465,11 +518,14 @@ mod tests {
|
|||||||
let Some(f) = font() else { return };
|
let Some(f) = font() else { return };
|
||||||
let len = || GLYPH_CACHE.with(|c| c.borrow().len());
|
let len = || GLYPH_CACHE.with(|c| c.borrow().len());
|
||||||
let before = len();
|
let before = len();
|
||||||
let _ = f.glyph_bitmap(42, 33.0, false);
|
let _ = f.glyph_bitmap(42, 33.0, false, false);
|
||||||
let _ = f.glyph_bitmap(42, 33.0, false);
|
let _ = f.glyph_bitmap(42, 33.0, false, false);
|
||||||
assert_eq!(len(), before + 1, "stesso glifo e dimensione: una voce");
|
assert_eq!(len(), before + 1, "stesso glifo e dimensione: una voce");
|
||||||
let _ = f.glyph_bitmap(42, 35.0, false);
|
let _ = f.glyph_bitmap(42, 35.0, false, false);
|
||||||
assert_eq!(len(), before + 2);
|
assert_eq!(len(), before + 2);
|
||||||
|
// il corsivo è una voce di cache distinta: glifi diversi, stessa chiave
|
||||||
|
let _ = f.glyph_bitmap(42, 35.0, false, true);
|
||||||
|
assert_eq!(len(), before + 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -488,8 +544,32 @@ mod tests {
|
|||||||
20.0,
|
20.0,
|
||||||
Color::BLACK,
|
Color::BLACK,
|
||||||
400,
|
400,
|
||||||
|
false,
|
||||||
);
|
);
|
||||||
let dark = buf.chunks(4).filter(|p| p[0] < 128).count();
|
let dark = buf.chunks(4).filter(|p| p[0] < 128).count();
|
||||||
assert!(dark > 50, "pixel scuri: {}", dark);
|
assert!(dark > 50, "pixel scuri: {}", dark);
|
||||||
|
|
||||||
|
// corsivo sintetico: inclinato, quindi l'inchiostro si sposta a destra in
|
||||||
|
// alto e la larghezza cresce rispetto al tondo
|
||||||
|
let mut buf_corsivo = vec![255u8; 200 * 40 * 4];
|
||||||
|
f.draw_shaped(
|
||||||
|
&mut buf_corsivo,
|
||||||
|
200,
|
||||||
|
40,
|
||||||
|
4.0,
|
||||||
|
25.0,
|
||||||
|
&shaped,
|
||||||
|
20.0,
|
||||||
|
20.0,
|
||||||
|
Color::BLACK,
|
||||||
|
400,
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
let dark_corsivo = buf_corsivo.chunks(4).filter(|p| p[0] < 128).count();
|
||||||
|
assert!(dark_corsivo > 50, "corsivo: pixel scuri {}", dark_corsivo);
|
||||||
|
assert_ne!(
|
||||||
|
buf, buf_corsivo,
|
||||||
|
"il corsivo deve produrre un disegno diverso dal tondo"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+71
-8
@@ -10,9 +10,27 @@ use crate::font::{FontHolder, Fonts, ShapedText};
|
|||||||
use crate::img::{ImageCache, ImageData};
|
use crate::img::{ImageCache, ImageData};
|
||||||
use crate::style::{
|
use crate::style::{
|
||||||
AlignItems, Color, ComputedStyle, Display, FlexDirection, FlexWrap, FloatSide, JustifyContent,
|
AlignItems, Color, ComputedStyle, Display, FlexDirection, FlexWrap, FloatSide, JustifyContent,
|
||||||
Position, TextAlign, Traccia, WhiteSpace,
|
Position, TextAlign, TextTransform, Traccia, WhiteSpace,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// Applica `min-height`/`max-height` a un'altezza del contenuto.
|
||||||
|
fn altezza_vincolata(st: &ComputedStyle, altezza: f32, base: f32) -> f32 {
|
||||||
|
let mut h = altezza;
|
||||||
|
if let Some(minimo) = st.min_height {
|
||||||
|
let m = minimo.resolve(base);
|
||||||
|
if h < m {
|
||||||
|
h = m;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if let Some(massimo) = st.max_height {
|
||||||
|
let m = massimo.resolve(base);
|
||||||
|
if m > 0.0 && h > m {
|
||||||
|
h = m;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
h.max(0.0)
|
||||||
|
}
|
||||||
|
|
||||||
thread_local! {
|
thread_local! {
|
||||||
/// densità dello schermo (1.0 = un pixel CSS per pixel del dispositivo).
|
/// densità dello schermo (1.0 = un pixel CSS per pixel del dispositivo).
|
||||||
/// Serve a `srcset`: con schermo denso si sceglie l'immagine più definita.
|
/// Serve a `srcset`: con schermo denso si sceglie l'immagine più definita.
|
||||||
@@ -71,6 +89,10 @@ pub struct TextRun {
|
|||||||
pub color: Color,
|
pub color: Color,
|
||||||
pub font_size: f32,
|
pub font_size: f32,
|
||||||
pub font_weight: u16,
|
pub font_weight: u16,
|
||||||
|
/// corsivo sintetico
|
||||||
|
pub italic: bool,
|
||||||
|
pub underline: bool,
|
||||||
|
pub line_through: bool,
|
||||||
/// se presente, il run è un controllo di modulo disegnato dal motore
|
/// se presente, il run è un controllo di modulo disegnato dal motore
|
||||||
pub control: Option<ControlSpec>,
|
pub control: Option<ControlSpec>,
|
||||||
}
|
}
|
||||||
@@ -389,6 +411,12 @@ impl<'a> Ctx<'a> {
|
|||||||
Some(l) => l.resolve(available_w),
|
Some(l) => l.resolve(available_w),
|
||||||
None => (available_w - bw[1] - bw[3] - pl - pr).max(0.0),
|
None => (available_w - bw[1] - bw[3] - pl - pr).max(0.0),
|
||||||
};
|
};
|
||||||
|
if let Some(minimo) = st.min_width {
|
||||||
|
let m = minimo.resolve(available_w);
|
||||||
|
if content_w < m {
|
||||||
|
content_w = m;
|
||||||
|
}
|
||||||
|
}
|
||||||
// max-width: il contenuto non si stende oltre il limite dichiarato
|
// max-width: il contenuto non si stende oltre il limite dichiarato
|
||||||
if let Some(massimo) = st.max_width {
|
if let Some(massimo) = st.max_width {
|
||||||
let m = massimo.resolve(available_w);
|
let m = massimo.resolve(available_w);
|
||||||
@@ -430,10 +458,14 @@ impl<'a> Ctx<'a> {
|
|||||||
} else {
|
} else {
|
||||||
self.layout_grid_children(node, content_x, content_y, content_w, &st)
|
self.layout_grid_children(node, content_x, content_y, content_w, &st)
|
||||||
};
|
};
|
||||||
let content_h = match st.height {
|
let content_h = altezza_vincolata(
|
||||||
|
&st,
|
||||||
|
match st.height {
|
||||||
Some(h) => h.resolve(available_w),
|
Some(h) => h.resolve(available_w),
|
||||||
None => altezza,
|
None => altezza,
|
||||||
};
|
},
|
||||||
|
available_w,
|
||||||
|
);
|
||||||
return LayoutBox {
|
return LayoutBox {
|
||||||
node,
|
node,
|
||||||
tag,
|
tag,
|
||||||
@@ -644,6 +676,9 @@ impl<'a> Ctx<'a> {
|
|||||||
if let Some(h) = st.height {
|
if let Some(h) = st.height {
|
||||||
content_h = h.resolve(available_w);
|
content_h = h.resolve(available_w);
|
||||||
}
|
}
|
||||||
|
// `min-height`/`max-height` valgono anche qui: è il percorso normale dei
|
||||||
|
// blocchi, e senza questo venivano applicati solo ad alcuni contenitori
|
||||||
|
content_h = altezza_vincolata(&st, content_h, available_w);
|
||||||
|
|
||||||
// Assoluti agganciati a questo box: ora che l'altezza è nota si possono
|
// Assoluti agganciati a questo box: ora che l'altezza è nota si possono
|
||||||
// posizionare. `bottom`/`right` senza `top`/`left` non sono supportati.
|
// posizionare. `bottom`/`right` senza `top`/`left` non sono supportati.
|
||||||
@@ -1192,6 +1227,12 @@ impl<'a> Ctx<'a> {
|
|||||||
Some(l) => l.resolve(available_w),
|
Some(l) => l.resolve(available_w),
|
||||||
None => (available_w - bw[1] - bw[3] - pl - pr).max(0.0),
|
None => (available_w - bw[1] - bw[3] - pl - pr).max(0.0),
|
||||||
};
|
};
|
||||||
|
if let Some(minimo) = st.min_width {
|
||||||
|
let m = minimo.resolve(available_w);
|
||||||
|
if content_w < m {
|
||||||
|
content_w = m;
|
||||||
|
}
|
||||||
|
}
|
||||||
// max-width: il contenuto non si stende oltre il limite dichiarato
|
// max-width: il contenuto non si stende oltre il limite dichiarato
|
||||||
if let Some(massimo) = st.max_width {
|
if let Some(massimo) = st.max_width {
|
||||||
let m = massimo.resolve(available_w);
|
let m = massimo.resolve(available_w);
|
||||||
@@ -1441,10 +1482,14 @@ impl<'a> Ctx<'a> {
|
|||||||
y += altezza_riga;
|
y += altezza_riga;
|
||||||
}
|
}
|
||||||
|
|
||||||
let content_h = match st.height {
|
let content_h = altezza_vincolata(
|
||||||
|
&st,
|
||||||
|
match st.height {
|
||||||
Some(h) => h.resolve(available_w),
|
Some(h) => h.resolve(available_w),
|
||||||
None => (y - content_y).max(0.0),
|
None => (y - content_y).max(0.0),
|
||||||
};
|
},
|
||||||
|
available_w,
|
||||||
|
);
|
||||||
LayoutBox {
|
LayoutBox {
|
||||||
node,
|
node,
|
||||||
tag,
|
tag,
|
||||||
@@ -2493,11 +2538,26 @@ fn place_word(
|
|||||||
// parola si compone con il font predefinito. Serve per i testi misti: il corsivo
|
// parola si compone con il font predefinito. Serve per i testi misti: il corsivo
|
||||||
// o il serif spesso non coprono ebraico o arabo.
|
// o il serif spesso non coprono ebraico o arabo.
|
||||||
let mut run_font = std::rc::Rc::clone(font);
|
let mut run_font = std::rc::Rc::clone(font);
|
||||||
let mut shaped = run_font.shape_dir(word, style.font_size, rtl);
|
// `text-transform` cambia il testo *prima* del shaping: maiuscole e minuscole
|
||||||
if shaped.glyphs.iter().any(|g| g.id == 0) && !word.is_empty() {
|
// possono avere avanzamenti diversi, quindi va fatto qui e non quando si disegna
|
||||||
|
let trasformata: String = match style.text_transform {
|
||||||
|
TextTransform::Uppercase => word.to_uppercase(),
|
||||||
|
TextTransform::Lowercase => word.to_lowercase(),
|
||||||
|
TextTransform::Capitalize => {
|
||||||
|
let mut c = word.chars();
|
||||||
|
match c.next() {
|
||||||
|
Some(primo) => primo.to_uppercase().collect::<String>() + c.as_str(),
|
||||||
|
None => String::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
TextTransform::None => word.to_string(),
|
||||||
|
};
|
||||||
|
let parola = trasformata.as_str();
|
||||||
|
let mut shaped = run_font.shape_spaced(parola, style.font_size, rtl, style.letter_spacing);
|
||||||
|
if shaped.glyphs.iter().any(|g| g.id == 0) && !parola.is_empty() {
|
||||||
let ripiego = fonts.default_font();
|
let ripiego = fonts.default_font();
|
||||||
if !std::rc::Rc::ptr_eq(&ripiego, font) {
|
if !std::rc::Rc::ptr_eq(&ripiego, font) {
|
||||||
let alt = ripiego.shape_dir(word, style.font_size, rtl);
|
let alt = ripiego.shape_spaced(parola, style.font_size, rtl, style.letter_spacing);
|
||||||
if !alt.glyphs.iter().any(|g| g.id == 0) {
|
if !alt.glyphs.iter().any(|g| g.id == 0) {
|
||||||
run_font = ripiego;
|
run_font = ripiego;
|
||||||
shaped = alt;
|
shaped = alt;
|
||||||
@@ -2620,6 +2680,9 @@ fn build_runs(
|
|||||||
color: p.style.color,
|
color: p.style.color,
|
||||||
font_size: p.style.font_size,
|
font_size: p.style.font_size,
|
||||||
font_weight: p.style.font_weight,
|
font_weight: p.style.font_weight,
|
||||||
|
italic: p.style.italic,
|
||||||
|
underline: p.style.underline,
|
||||||
|
line_through: p.style.line_through,
|
||||||
control: p.control.clone(),
|
control: p.control.clone(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
+15
-1
@@ -237,7 +237,21 @@ fn render_pezzi(
|
|||||||
|
|
||||||
let t3 = std::time::Instant::now();
|
let t3 = std::time::Instant::now();
|
||||||
let mut cmds = Vec::new();
|
let mut cmds = Vec::new();
|
||||||
paint::build_display_list(&layout, opts.viewport.1 as f32 / opts.dpi_scale, &mut cmds);
|
// La lista di disegno ha bisogno di una cache immagini per gli sfondi
|
||||||
|
// `background-image`. NON si può riusare `images`: `layout_document` se la
|
||||||
|
// porta via (`mem::replace`) e ne lascia una finta con `base_dir`, quindi qui
|
||||||
|
// la cache va ricostruita con la cartella giusta — altrimenti gli sfondi
|
||||||
|
// relativi si cercano in "." e non si trovano.
|
||||||
|
let cache_immagini =
|
||||||
|
std::cell::RefCell::new(img::ImageCache::new(&opts.base_dir, Some(risorse.clone())));
|
||||||
|
paint::build_display_list(
|
||||||
|
&layout,
|
||||||
|
opts.viewport.0 as f32 / opts.dpi_scale,
|
||||||
|
opts.viewport.1 as f32 / opts.dpi_scale,
|
||||||
|
&mut cmds,
|
||||||
|
&cache_immagini,
|
||||||
|
);
|
||||||
|
|
||||||
let display_ms = t3.elapsed().as_secs_f64() * 1000.0;
|
let display_ms = t3.elapsed().as_secs_f64() * 1000.0;
|
||||||
let t3b = std::time::Instant::now();
|
let t3b = std::time::Instant::now();
|
||||||
let (w, mut h) = (opts.viewport.0, opts.viewport.1);
|
let (w, mut h) = (opts.viewport.0, opts.viewport.1);
|
||||||
|
|||||||
+375
-31
@@ -15,12 +15,13 @@ pub enum Cmd {
|
|||||||
Rect {
|
Rect {
|
||||||
rect: Rect,
|
rect: Rect,
|
||||||
color: Color,
|
color: Color,
|
||||||
|
/// `border-radius` in px CSS (0 = angoli retti)
|
||||||
|
radius: f32,
|
||||||
},
|
},
|
||||||
/// Immagine sostituita, già scalata al rettangolo.
|
/// Immagine sostituita, già scalata al rettangolo.
|
||||||
Image {
|
Image { rect: Rect, data: Arc<ImageData> },
|
||||||
rect: Rect,
|
/// Sfondo a gradiente lineare, con il testo CSS originale.
|
||||||
data: Arc<ImageData>,
|
Gradient { rect: Rect, css: String, alpha: f32 },
|
||||||
},
|
|
||||||
/// Controllo di modulo: il motore disegna la cornice e il valore.
|
/// Controllo di modulo: il motore disegna la cornice e il valore.
|
||||||
Control {
|
Control {
|
||||||
rect: Rect,
|
rect: Rect,
|
||||||
@@ -62,10 +63,23 @@ pub enum Cmd {
|
|||||||
size: f32,
|
size: f32,
|
||||||
weight: u16,
|
weight: u16,
|
||||||
color: Color,
|
color: Color,
|
||||||
|
/// corsivo sintetico: i glifi si disegnano inclinati
|
||||||
|
italic: bool,
|
||||||
|
/// sottolineato e barrato (px CSS)
|
||||||
|
underline: bool,
|
||||||
|
line_through: bool,
|
||||||
|
/// larghezza del testo, per la linea di decorazione
|
||||||
|
width: f32,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn build_display_list(root: &LayoutBox, altezza_viewport: f32, out: &mut Vec<Cmd>) {
|
pub fn build_display_list(
|
||||||
|
root: &LayoutBox,
|
||||||
|
larghezza_viewport: f32,
|
||||||
|
altezza_viewport: f32,
|
||||||
|
out: &mut Vec<Cmd>,
|
||||||
|
immagini: &std::cell::RefCell<crate::img::ImageCache>,
|
||||||
|
) {
|
||||||
// gli elenchi aperti stanno sopra ogni altro comando
|
// gli elenchi aperti stanno sopra ogni altro comando
|
||||||
let mut popups: Vec<Cmd> = Vec::new();
|
let mut popups: Vec<Cmd> = Vec::new();
|
||||||
let altezza_documento = if altezza_viewport > 0.0 {
|
let altezza_documento = if altezza_viewport > 0.0 {
|
||||||
@@ -73,33 +87,106 @@ pub fn build_display_list(root: &LayoutBox, altezza_viewport: f32, out: &mut Vec
|
|||||||
} else {
|
} else {
|
||||||
root.rect.h
|
root.rect.h
|
||||||
};
|
};
|
||||||
walk(root, out, &mut popups, altezza_documento);
|
// Lo sfondo di `html` (o, se trasparente, quello di `body`) si propaga alla
|
||||||
|
// tela: è la regola che colora la finestra intera e non solo il box.
|
||||||
|
if let Some(colore) = colore_della_tela(root) {
|
||||||
|
let larghezza = larghezza_viewport.max(root.rect.w).max(1.0);
|
||||||
|
let altezza = altezza_documento.max(root.rect.h).max(1.0);
|
||||||
|
out.push(Cmd::Rect {
|
||||||
|
rect: Rect::new(0.0, 0.0, larghezza, altezza),
|
||||||
|
color: colore,
|
||||||
|
radius: 0.0,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
walk(
|
||||||
|
root,
|
||||||
|
out,
|
||||||
|
&mut popups,
|
||||||
|
altezza_documento,
|
||||||
|
1.0,
|
||||||
|
None,
|
||||||
|
immagini,
|
||||||
|
);
|
||||||
out.extend(popups);
|
out.extend(popups);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn walk(b: &LayoutBox, out: &mut Vec<Cmd>, popups: &mut Vec<Cmd>, altezza_documento: f32) {
|
fn walk(
|
||||||
|
b: &LayoutBox,
|
||||||
|
out: &mut Vec<Cmd>,
|
||||||
|
popups: &mut Vec<Cmd>,
|
||||||
|
altezza_documento: f32,
|
||||||
|
alpha: f32,
|
||||||
|
clip: Option<Rect>,
|
||||||
|
immagini: &std::cell::RefCell<crate::img::ImageCache>,
|
||||||
|
) {
|
||||||
let bw = b.style.border_width;
|
let bw = b.style.border_width;
|
||||||
|
// `opacity` si moltiplica lungo l'albero: un figlio al 50% dentro un padre al
|
||||||
|
// 50% è al 25%, come nel browser
|
||||||
|
let alpha = (alpha * b.style.opacity).clamp(0.0, 1.0);
|
||||||
|
// `overflow: hidden` ritaglia i discendenti: il ritaglio è un rettangolo che
|
||||||
|
// si interseca scendendo, quindi basta tenerne uno solo
|
||||||
|
let clip = if b.style.clip {
|
||||||
|
let mio = b.rect.inset(bw[0], bw[1], bw[2], bw[3]);
|
||||||
|
Some(match clip {
|
||||||
|
Some(c) => interseca(c, mio),
|
||||||
|
None => mio,
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
clip
|
||||||
|
};
|
||||||
|
let raggio = b.style.border_radius;
|
||||||
|
|
||||||
|
// comodo: applica ritaglio e opacità a un rettangolo
|
||||||
|
let mut rett = |out: &mut Vec<Cmd>, rect: Rect, color: Color, radius: f32| {
|
||||||
|
let Some(rect) = applica_clip(rect, clip) else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
let colore = con_alpha(color, alpha);
|
||||||
|
if colore.a == 0 || rect.w <= 0.0 || rect.h <= 0.0 {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
out.push(Cmd::Rect {
|
||||||
|
rect,
|
||||||
|
color: colore,
|
||||||
|
radius,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
// Elemento sostituito (immagine come blocco o float): prima dello sfondo.
|
// Elemento sostituito (immagine come blocco o float): prima dello sfondo.
|
||||||
if let Some((data, _, _)) = &b.replaced {
|
if let Some((data, _, _)) = &b.replaced {
|
||||||
|
if let Some(rect) = applica_clip(b.rect, clip) {
|
||||||
out.push(Cmd::Image {
|
out.push(Cmd::Image {
|
||||||
rect: b.rect,
|
rect,
|
||||||
data: Arc::clone(data),
|
data: Arc::clone(data),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Sfondo: padding box (il bordo lo copre).
|
// Sfondo: padding box (il bordo lo copre). Prima l'immagine o il gradiente,
|
||||||
if b.style.background.a > 0 {
|
// poi il colore: è l'ordine del CSS (il colore sta sotto).
|
||||||
let inner = b.rect.inset(bw[0], bw[1], bw[2], bw[3]);
|
let inner = b.rect.inset(bw[0], bw[1], bw[2], bw[3]);
|
||||||
if inner.w > 0.0 && inner.h > 0.0 {
|
if let Some(url) = &b.style.background_image {
|
||||||
out.push(Cmd::Rect {
|
if let Some(data) = immagini.borrow_mut().get(url) {
|
||||||
rect: inner,
|
if let Some(rect) = applica_clip(inner, clip) {
|
||||||
color: b.style.background,
|
out.push(Cmd::Image { rect, data });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if let Some(gradiente) = &b.style.background_gradient {
|
||||||
|
if let Some(rect) = applica_clip(inner, clip) {
|
||||||
|
out.push(Cmd::Gradient {
|
||||||
|
rect,
|
||||||
|
css: gradiente.clone(),
|
||||||
|
alpha,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if b.style.background.a > 0 {
|
||||||
|
rett(out, inner, b.style.background, raggio);
|
||||||
|
}
|
||||||
|
|
||||||
// Bordi: quattro rettangoli, nessun raggio in M0.
|
// Bordi: quattro rettangoli. Con un raggio il disegno resta a spigoli, ma
|
||||||
|
// sfondo e immagini sono già arrotondati: la differenza si vede solo sui
|
||||||
|
// bordi spessi, ed è dichiarata.
|
||||||
let r = b.rect;
|
let r = b.rect;
|
||||||
let sides = [
|
let sides = [
|
||||||
Rect::new(r.x, r.y, r.w, bw[0]),
|
Rect::new(r.x, r.y, r.w, bw[0]),
|
||||||
@@ -108,11 +195,8 @@ fn walk(b: &LayoutBox, out: &mut Vec<Cmd>, popups: &mut Vec<Cmd>, altezza_docume
|
|||||||
Rect::new(r.x, r.y, bw[3], r.h),
|
Rect::new(r.x, r.y, bw[3], r.h),
|
||||||
];
|
];
|
||||||
for (i, side) in sides.iter().enumerate() {
|
for (i, side) in sides.iter().enumerate() {
|
||||||
if bw[i] > 0.0 && b.style.border_color[i].a > 0 && side.w > 0.0 && side.h > 0.0 {
|
if bw[i] > 0.0 && b.style.border_color[i].a > 0 {
|
||||||
out.push(Cmd::Rect {
|
rett(out, *side, b.style.border_color[i], 0.0);
|
||||||
rect: *side,
|
|
||||||
color: b.style.border_color[i],
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -173,12 +257,64 @@ fn walk(b: &LayoutBox, out: &mut Vec<Cmd>, popups: &mut Vec<Cmd>, altezza_docume
|
|||||||
size: run.font_size,
|
size: run.font_size,
|
||||||
weight: run.font_weight,
|
weight: run.font_weight,
|
||||||
color: run.color,
|
color: run.color,
|
||||||
|
italic: run.italic,
|
||||||
|
underline: run.underline,
|
||||||
|
line_through: run.line_through,
|
||||||
|
width: run.width,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for c in &b.children {
|
for c in &b.children {
|
||||||
walk(c, out, popups, altezza_documento);
|
walk(c, out, popups, altezza_documento, alpha, clip, immagini);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Colore da stendere sulla tela: quello di `html`, altrimenti quello di `body`.
|
||||||
|
fn colore_della_tela(radice: &LayoutBox) -> Option<Color> {
|
||||||
|
let opaco = |c: &Color| c.a > 0;
|
||||||
|
if opaco(&radice.style.background) {
|
||||||
|
return Some(radice.style.background);
|
||||||
|
}
|
||||||
|
for figlio in &radice.children {
|
||||||
|
if figlio.tag.eq_ignore_ascii_case("body") && opaco(&figlio.style.background) {
|
||||||
|
return Some(figlio.style.background);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Intersezione di due rettangoli (per il ritaglio annidato).
|
||||||
|
fn interseca(a: Rect, b: Rect) -> Rect {
|
||||||
|
let x = a.x.max(b.x);
|
||||||
|
let y = a.y.max(b.y);
|
||||||
|
let destra = a.right().min(b.right());
|
||||||
|
let sotto = a.bottom().min(b.bottom());
|
||||||
|
Rect::new(x, y, (destra - x).max(0.0), (sotto - y).max(0.0))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Ritaglia un rettangolo, oppure lo scarta se non resta niente da disegnare.
|
||||||
|
fn applica_clip(rect: Rect, clip: Option<Rect>) -> Option<Rect> {
|
||||||
|
match clip {
|
||||||
|
Some(c) => {
|
||||||
|
let r = interseca(rect, c);
|
||||||
|
if r.w > 0.0 && r.h > 0.0 {
|
||||||
|
Some(r)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => Some(rect),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Colore con l'opacità accumulata.
|
||||||
|
fn con_alpha(color: Color, alpha: f32) -> Color {
|
||||||
|
Color {
|
||||||
|
r: color.r,
|
||||||
|
g: color.g,
|
||||||
|
b: color.b,
|
||||||
|
a: ((color.a as f32) * alpha).round().clamp(0.0, 255.0) as u8,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -193,22 +329,28 @@ pub fn rasterize(cmds: &[Cmd], w: u32, h: u32, scale: f32) -> Vec<u8> {
|
|||||||
let mut buf = vec![255u8; (w as usize) * (h as usize) * 4];
|
let mut buf = vec![255u8; (w as usize) * (h as usize) * 4];
|
||||||
for cmd in cmds {
|
for cmd in cmds {
|
||||||
match cmd {
|
match cmd {
|
||||||
Cmd::Rect { rect, color } => {
|
Cmd::Gradient { rect, css, alpha } => {
|
||||||
|
disegna_gradiente(&mut buf, w, h, *rect, css, *alpha, scale);
|
||||||
|
}
|
||||||
|
Cmd::Rect {
|
||||||
|
rect,
|
||||||
|
color,
|
||||||
|
radius,
|
||||||
|
} => {
|
||||||
if stats {
|
if stats {
|
||||||
rect_px += (rect.w * scale).max(0.0) as u64 * (rect.h * scale).max(0.0) as u64;
|
rect_px += (rect.w * scale).max(0.0) as u64 * (rect.h * scale).max(0.0) as u64;
|
||||||
}
|
}
|
||||||
fill_rect(
|
let scalato = Rect::new(
|
||||||
&mut buf,
|
|
||||||
w,
|
|
||||||
h,
|
|
||||||
Rect::new(
|
|
||||||
rect.x * scale,
|
rect.x * scale,
|
||||||
rect.y * scale,
|
rect.y * scale,
|
||||||
rect.w * scale,
|
rect.w * scale,
|
||||||
rect.h * scale,
|
rect.h * scale,
|
||||||
),
|
|
||||||
*color,
|
|
||||||
);
|
);
|
||||||
|
if *radius > 0.0 {
|
||||||
|
fill_rect_arrotondato(&mut buf, w, h, scalato, *color, radius * scale);
|
||||||
|
} else {
|
||||||
|
fill_rect(&mut buf, w, h, scalato, *color);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Cmd::Image { rect, data } => {
|
Cmd::Image { rect, data } => {
|
||||||
crate::img::draw_image(
|
crate::img::draw_image(
|
||||||
@@ -308,6 +450,7 @@ pub fn rasterize(cmds: &[Cmd], w: u32, h: u32, scale: f32) -> Vec<u8> {
|
|||||||
px,
|
px,
|
||||||
colore,
|
colore,
|
||||||
400,
|
400,
|
||||||
|
false,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
frame_rect(
|
frame_rect(
|
||||||
@@ -375,6 +518,10 @@ pub fn rasterize(cmds: &[Cmd], w: u32, h: u32, scale: f32) -> Vec<u8> {
|
|||||||
size,
|
size,
|
||||||
weight,
|
weight,
|
||||||
color,
|
color,
|
||||||
|
italic,
|
||||||
|
underline,
|
||||||
|
line_through,
|
||||||
|
width,
|
||||||
} => {
|
} => {
|
||||||
if stats {
|
if stats {
|
||||||
text_cmds += 1;
|
text_cmds += 1;
|
||||||
@@ -391,7 +538,21 @@ pub fn rasterize(cmds: &[Cmd], w: u32, h: u32, scale: f32) -> Vec<u8> {
|
|||||||
size * scale,
|
size * scale,
|
||||||
*color,
|
*color,
|
||||||
*weight,
|
*weight,
|
||||||
|
*italic,
|
||||||
);
|
);
|
||||||
|
// decorazioni: uno spessore proporzionato al corpo del testo,
|
||||||
|
// sottolineato poco sotto la baseline, barrato a metà altezza
|
||||||
|
let spessore = (size * scale * 0.06).max(1.0);
|
||||||
|
if *underline {
|
||||||
|
let y = (baseline + size * 0.12) * scale;
|
||||||
|
let r = Rect::new(x * scale, y, width * scale, spessore);
|
||||||
|
fill_rect(&mut buf, w, h, r, *color);
|
||||||
|
}
|
||||||
|
if *line_through {
|
||||||
|
let y = (baseline - size * 0.3) * scale;
|
||||||
|
let r = Rect::new(x * scale, y, width * scale, spessore);
|
||||||
|
fill_rect(&mut buf, w, h, r, *color);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -590,13 +751,13 @@ fn draw_control(
|
|||||||
}
|
}
|
||||||
let shaped = font.shape_dir(riga, px, false);
|
let shaped = font.shape_dir(riga, px, false);
|
||||||
font.draw_shaped_in(
|
font.draw_shaped_in(
|
||||||
buf, w, h, x_testo, base_riga, &shaped, px, px, color, 400, clip,
|
buf, w, h, x_testo, base_riga, &shaped, px, px, color, 400, false, clip,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} else if !text.is_empty() {
|
} else if !text.is_empty() {
|
||||||
let shaped = font.shape_dir(text, px, false);
|
let shaped = font.shape_dir(text, px, false);
|
||||||
font.draw_shaped_in(
|
font.draw_shaped_in(
|
||||||
buf, w, h, x_testo, baseline, &shaped, px, px, color, 400, clip,
|
buf, w, h, x_testo, baseline, &shaped, px, px, color, 400, false, clip,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -704,3 +865,186 @@ fn fill_rect(buf: &mut [u8], w: u32, h: u32, rect: Rect, color: Color) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Rettangolo con angoli arrotondati: per ogni riga si restringe il segmento
|
||||||
|
/// secondo il cerchio del raggio. Il raggio viene limitato a metà lato.
|
||||||
|
fn fill_rect_arrotondato(buf: &mut [u8], w: u32, h: u32, rect: Rect, color: Color, raggio: f32) {
|
||||||
|
let r = raggio.min(rect.w / 2.0).min(rect.h / 2.0).max(0.0);
|
||||||
|
if r <= 0.5 {
|
||||||
|
fill_rect(buf, w, h, rect, color);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let y0 = rect.y.floor().max(0.0) as i32;
|
||||||
|
let y1 = (rect.y + rect.h).ceil().min(h as f32) as i32;
|
||||||
|
for y in y0..y1 {
|
||||||
|
let centro_y = y as f32 + 0.5;
|
||||||
|
// distanza dal centro del cerchio più vicino, in verticale
|
||||||
|
let dy = if centro_y < rect.y + r {
|
||||||
|
rect.y + r - centro_y
|
||||||
|
} else if centro_y > rect.y + rect.h - r {
|
||||||
|
centro_y - (rect.y + rect.h - r)
|
||||||
|
} else {
|
||||||
|
0.0
|
||||||
|
};
|
||||||
|
let rientro = if dy > 0.0 {
|
||||||
|
let dentro = (r * r - dy * dy).max(0.0).sqrt();
|
||||||
|
r - dentro
|
||||||
|
} else {
|
||||||
|
0.0
|
||||||
|
};
|
||||||
|
let riga = Rect::new(rect.x + rientro, y as f32, rect.w - 2.0 * rientro, 1.0);
|
||||||
|
fill_rect(buf, w, h, riga, color);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Gradiente lineare: `to bottom|right|top|left` oppure un angolo in gradi,
|
||||||
|
/// con due o più colori e fermate percentuali facoltative.
|
||||||
|
fn disegna_gradiente(
|
||||||
|
buf: &mut [u8],
|
||||||
|
w: u32,
|
||||||
|
h: u32,
|
||||||
|
rect: Rect,
|
||||||
|
css: &str,
|
||||||
|
alpha: f32,
|
||||||
|
scale: f32,
|
||||||
|
) {
|
||||||
|
let (direzione, fermate) = match analizza_gradiente(css) {
|
||||||
|
Some(v) if v.1.len() >= 2 => v,
|
||||||
|
_ => return,
|
||||||
|
};
|
||||||
|
let (dx, dy) = direzione;
|
||||||
|
// proiezione sul versore: 0 all'inizio, 1 alla fine
|
||||||
|
let (x0, y0) = (rect.x * scale, rect.y * scale);
|
||||||
|
let (bw, bh) = (rect.w * scale, rect.h * scale);
|
||||||
|
// lunghezza della proiezione del rettangolo sul versore
|
||||||
|
let lunghezza = (dx.abs() * bw + dy.abs() * bh).max(1.0);
|
||||||
|
let partenza =
|
||||||
|
(if dx < 0.0 { bw } else { 0.0 }) * dx.abs() + (if dy < 0.0 { bh } else { 0.0 }) * dy.abs();
|
||||||
|
|
||||||
|
let ya = y0.floor().max(0.0) as i32;
|
||||||
|
let yb = (y0 + bh).ceil().min(h as f32) as i32;
|
||||||
|
let xa = x0.floor().max(0.0) as i32;
|
||||||
|
let xb = (x0 + bw).ceil().min(w as f32) as i32;
|
||||||
|
for y in ya..yb {
|
||||||
|
for x in xa..xb {
|
||||||
|
let px = x as f32 + 0.5 - x0;
|
||||||
|
let py = y as f32 + 0.5 - y0;
|
||||||
|
let proiezione = (px * dx + py * dy - partenza) / lunghezza;
|
||||||
|
let colore = colore_alla_fermata(&fermate, proiezione.clamp(0.0, 1.0));
|
||||||
|
let i = ((y as u32 * w + x as u32) * 4) as usize;
|
||||||
|
if i + 3 >= buf.len() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let a = (colore.a as f32 / 255.0) * alpha;
|
||||||
|
misto(&mut buf[i..i + 4], colore, a);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `linear-gradient(...)` → versore della direzione e fermate `(posizione, colore)`.
|
||||||
|
fn analizza_gradiente(css: &str) -> Option<((f32, f32), Vec<(f32, Color)>)> {
|
||||||
|
let inizio = css.find("gradient(")? + "gradient(".len();
|
||||||
|
let dentro = &css[inizio..css.rfind(')')?];
|
||||||
|
let mut parti = dentro.split(',').map(|p| p.trim().to_string());
|
||||||
|
let prima = parti.next()?;
|
||||||
|
let (direzione, resto): ((f32, f32), Vec<String>) = {
|
||||||
|
let minuscola = prima.to_ascii_lowercase();
|
||||||
|
if let Some(angolo) = minuscola.strip_suffix("deg") {
|
||||||
|
let g: f32 = angolo.trim().parse().ok()?;
|
||||||
|
let r = g.to_radians();
|
||||||
|
// 0deg = verso l'alto, in senso orario
|
||||||
|
((r.sin(), -r.cos()), parti.collect())
|
||||||
|
} else if minuscola.starts_with("to ") {
|
||||||
|
let d = match minuscola.trim_start_matches("to ").trim() {
|
||||||
|
"right" => (1.0, 0.0),
|
||||||
|
"left" => (-1.0, 0.0),
|
||||||
|
"top" => (0.0, -1.0),
|
||||||
|
"bottom" => (0.0, 1.0),
|
||||||
|
"top right" | "right top" => (0.707, -0.707),
|
||||||
|
"top left" | "left top" => (-0.707, -0.707),
|
||||||
|
"bottom right" | "right bottom" => (0.707, 0.707),
|
||||||
|
"bottom left" | "left bottom" => (-0.707, 0.707),
|
||||||
|
_ => (0.0, 1.0),
|
||||||
|
};
|
||||||
|
(d, parti.collect())
|
||||||
|
} else {
|
||||||
|
// nessuna direzione: la prima è già un colore, vale `to bottom`
|
||||||
|
let mut resto: Vec<String> = parti.collect();
|
||||||
|
resto.insert(0, prima.clone());
|
||||||
|
((0.0, 1.0), resto)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut fermate: Vec<(Option<f32>, Color)> = Vec::new();
|
||||||
|
for voce in resto {
|
||||||
|
let mut pezzi = voce.split_whitespace();
|
||||||
|
let colore_testo = pezzi.next()?;
|
||||||
|
let colore = crate::style::parse_color(colore_testo)?;
|
||||||
|
let posizione = pezzi.next().and_then(|p| {
|
||||||
|
p.trim()
|
||||||
|
.strip_suffix('%')
|
||||||
|
.and_then(|n| n.trim().parse::<f32>().ok())
|
||||||
|
.map(|n| n / 100.0)
|
||||||
|
});
|
||||||
|
fermate.push((posizione, colore));
|
||||||
|
}
|
||||||
|
if fermate.len() < 2 {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
// posizioni mancanti: si distribuiscono in modo uniforme
|
||||||
|
let n = fermate.len();
|
||||||
|
let mut posizioni: Vec<f32> = Vec::with_capacity(n);
|
||||||
|
for (i, (p, _)) in fermate.iter().enumerate() {
|
||||||
|
posizioni.push(p.unwrap_or(i as f32 / (n - 1) as f32));
|
||||||
|
}
|
||||||
|
// le posizioni dichiarate devono restare crescenti
|
||||||
|
for i in 1..n {
|
||||||
|
if posizioni[i] < posizioni[i - 1] {
|
||||||
|
posizioni[i] = posizioni[i - 1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some((
|
||||||
|
direzione,
|
||||||
|
posizioni
|
||||||
|
.into_iter()
|
||||||
|
.zip(fermate.into_iter())
|
||||||
|
.map(|(p, (_, c))| (p, c))
|
||||||
|
.collect(),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn colore_alla_fermata(fermate: &[(f32, Color)], t: f32) -> Color {
|
||||||
|
if t <= fermate[0].0 {
|
||||||
|
return fermate[0].1;
|
||||||
|
}
|
||||||
|
for coppia in fermate.windows(2) {
|
||||||
|
let (p0, c0) = coppia[0];
|
||||||
|
let (p1, c1) = coppia[1];
|
||||||
|
if t <= p1 {
|
||||||
|
let k = if (p1 - p0).abs() < 1e-6 {
|
||||||
|
0.0
|
||||||
|
} else {
|
||||||
|
(t - p0) / (p1 - p0)
|
||||||
|
};
|
||||||
|
let mescola = |a: u8, b: u8| (a as f32 * (1.0 - k) + b as f32 * k).round() as u8;
|
||||||
|
return Color {
|
||||||
|
r: mescola(c0.r, c1.r),
|
||||||
|
g: mescola(c0.g, c1.g),
|
||||||
|
b: mescola(c0.b, c1.b),
|
||||||
|
a: mescola(c0.a, c1.a),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fermate[fermate.len() - 1].1
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Miscela un colore sul buffer (RGBA) con la sua opacità.
|
||||||
|
fn misto(pixel: &mut [u8], colore: Color, alpha: f32) {
|
||||||
|
if alpha <= 0.0 {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let a = alpha.clamp(0.0, 1.0);
|
||||||
|
pixel[0] = (colore.r as f32 * a + pixel[0] as f32 * (1.0 - a)).round() as u8;
|
||||||
|
pixel[1] = (colore.g as f32 * a + pixel[1] as f32 * (1.0 - a)).round() as u8;
|
||||||
|
pixel[2] = (colore.b as f32 * a + pixel[2] as f32 * (1.0 - a)).round() as u8;
|
||||||
|
}
|
||||||
+179
-1
@@ -88,6 +88,15 @@ pub enum Traccia {
|
|||||||
Auto,
|
Auto,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
|
||||||
|
pub enum TextTransform {
|
||||||
|
#[default]
|
||||||
|
None,
|
||||||
|
Uppercase,
|
||||||
|
Lowercase,
|
||||||
|
Capitalize,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||||
pub enum FlexDirection {
|
pub enum FlexDirection {
|
||||||
Row,
|
Row,
|
||||||
@@ -190,6 +199,30 @@ pub struct ComputedStyle {
|
|||||||
pub color: Color,
|
pub color: Color,
|
||||||
pub font_size: f32,
|
pub font_size: f32,
|
||||||
pub font_weight: u16,
|
pub font_weight: u16,
|
||||||
|
/// `font-style: italic`/`oblique`: il corsivo si sintetizza inclinando i glifi,
|
||||||
|
/// perché con un solo file di font per famiglia non c'è la faccia inclinata
|
||||||
|
pub italic: bool,
|
||||||
|
/// `text-decoration`: sottolineato e barrato si disegnano come linee
|
||||||
|
pub underline: bool,
|
||||||
|
pub line_through: bool,
|
||||||
|
/// `text-transform`
|
||||||
|
pub text_transform: TextTransform,
|
||||||
|
/// `letter-spacing`, in px CSS. Positivo o negativo.
|
||||||
|
pub letter_spacing: f32,
|
||||||
|
/// `opacity` dell'elemento e dei suoi discendenti
|
||||||
|
pub opacity: f32,
|
||||||
|
/// `border-radius` (un solo valore per tutti gli angoli)
|
||||||
|
pub border_radius: f32,
|
||||||
|
/// `min-height`/`max-height`/`min-width`
|
||||||
|
pub min_height: Option<Len>,
|
||||||
|
pub max_height: Option<Len>,
|
||||||
|
pub min_width: Option<Len>,
|
||||||
|
/// `overflow: hidden|clip`: i discendenti non escono dal box
|
||||||
|
pub clip: bool,
|
||||||
|
/// `background-image: url(...)`
|
||||||
|
pub background_image: Option<String>,
|
||||||
|
/// `background-image: linear-gradient(...)`, testo originale
|
||||||
|
pub background_gradient: Option<String>,
|
||||||
pub line_height: f32,
|
pub line_height: f32,
|
||||||
/// vero se `line-height` è stato dichiarato: altrimenti segue `1.2 * font-size`.
|
/// vero se `line-height` è stato dichiarato: altrimenti segue `1.2 * font-size`.
|
||||||
pub line_height_explicit: bool,
|
pub line_height_explicit: bool,
|
||||||
@@ -247,6 +280,19 @@ impl Default for ComputedStyle {
|
|||||||
color: Color::BLACK,
|
color: Color::BLACK,
|
||||||
font_size: 16.0,
|
font_size: 16.0,
|
||||||
font_weight: 400,
|
font_weight: 400,
|
||||||
|
italic: false,
|
||||||
|
underline: false,
|
||||||
|
line_through: false,
|
||||||
|
text_transform: TextTransform::None,
|
||||||
|
letter_spacing: 0.0,
|
||||||
|
opacity: 1.0,
|
||||||
|
border_radius: 0.0,
|
||||||
|
min_height: None,
|
||||||
|
max_height: None,
|
||||||
|
min_width: None,
|
||||||
|
clip: false,
|
||||||
|
background_image: None,
|
||||||
|
background_gradient: None,
|
||||||
line_height: 19.2,
|
line_height: 19.2,
|
||||||
line_height_explicit: false,
|
line_height_explicit: false,
|
||||||
text_align: TextAlign::Left,
|
text_align: TextAlign::Left,
|
||||||
@@ -323,6 +369,13 @@ th { font-weight: bold; text-align: center }
|
|||||||
thead, tbody, tfoot { display: block }
|
thead, tbody, tfoot { display: block }
|
||||||
span, a, b, i, em, strong, small, code, sub, sup, label, br, img,
|
span, a, b, i, em, strong, small, code, sub, sup, label, br, img,
|
||||||
u, s, cite, q, time, abbr, mark, var, kbd, samp { display: inline }
|
u, s, cite, q, time, abbr, mark, var, kbd, samp { display: inline }
|
||||||
|
a { color: #0000ee; text-decoration: underline }
|
||||||
|
a:visited { color: #551a8b }
|
||||||
|
b, strong { font-weight: bold }
|
||||||
|
em, i, cite, var, dfn { font-style: italic }
|
||||||
|
u { text-decoration: underline }
|
||||||
|
s, del, strike { text-decoration: line-through }
|
||||||
|
sub { font-size: 13px } sup { font-size: 13px }
|
||||||
"#;
|
"#;
|
||||||
|
|
||||||
pub fn ua_rules() -> &'static Vec<Rule> {
|
pub fn ua_rules() -> &'static Vec<Rule> {
|
||||||
@@ -695,6 +748,69 @@ fn apply_declaration(st: &mut ComputedStyle, name: &str, value: &str, parent_fon
|
|||||||
if let Some(c) = parse_color(v) {
|
if let Some(c) = parse_color(v) {
|
||||||
st.background = c;
|
st.background = c;
|
||||||
}
|
}
|
||||||
|
if v.contains("gradient(") {
|
||||||
|
st.background_gradient = Some(v.trim().to_string());
|
||||||
|
} else if let Some(url) = url_di_background(v) {
|
||||||
|
st.background_image = Some(url);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"font-style" => {
|
||||||
|
let v = v.trim().to_ascii_lowercase();
|
||||||
|
st.italic = v == "italic" || v == "oblique";
|
||||||
|
}
|
||||||
|
"font-synthesis" => { /* si sintetizza sempre: non c'è una faccia corsiva */ }
|
||||||
|
"text-decoration" | "text-decoration-line" => {
|
||||||
|
let v = v.trim().to_ascii_lowercase();
|
||||||
|
st.underline = v.split_whitespace().any(|p| p == "underline");
|
||||||
|
st.line_through = v.split_whitespace().any(|p| p == "line-through");
|
||||||
|
}
|
||||||
|
"text-transform" => {
|
||||||
|
st.text_transform = match v.trim().to_ascii_lowercase().as_str() {
|
||||||
|
"uppercase" => TextTransform::Uppercase,
|
||||||
|
"lowercase" => TextTransform::Lowercase,
|
||||||
|
"capitalize" => TextTransform::Capitalize,
|
||||||
|
_ => TextTransform::None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"letter-spacing" => {
|
||||||
|
if v.trim() == "normal" {
|
||||||
|
st.letter_spacing = 0.0;
|
||||||
|
} else if let Some(l) = parse_len(v, st.font_size) {
|
||||||
|
st.letter_spacing = l.resolve(st.font_size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"opacity" => {
|
||||||
|
if let Ok(n) = v.trim().parse::<f32>() {
|
||||||
|
st.opacity = n.clamp(0.0, 1.0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"border-radius" => {
|
||||||
|
// un solo valore: `10px`, `50%`, `10px 20px` si prende il primo
|
||||||
|
if let Some(primo) = v.split_whitespace().next() {
|
||||||
|
if let Some(l) = parse_len(primo, st.font_size) {
|
||||||
|
st.border_radius = l.resolve(st.font_size).max(0.0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"min-height" => st.min_height = parse_len(v, st.font_size),
|
||||||
|
"max-height" => st.max_height = parse_len(v, st.font_size),
|
||||||
|
"min-width" => st.min_width = parse_len(v, st.font_size),
|
||||||
|
"overflow" | "overflow-x" | "overflow-y" => {
|
||||||
|
let v = v.trim().to_ascii_lowercase();
|
||||||
|
st.clip = v.split_whitespace().any(|p| p == "hidden" || p == "clip");
|
||||||
|
}
|
||||||
|
"background-image" => {
|
||||||
|
let v = v.trim();
|
||||||
|
if v == "none" {
|
||||||
|
st.background_image = None;
|
||||||
|
st.background_gradient = None;
|
||||||
|
} else if v.contains("gradient(") {
|
||||||
|
st.background_gradient = Some(v.to_string());
|
||||||
|
st.background_image = None;
|
||||||
|
} else if let Some(url) = url_di_background(v) {
|
||||||
|
st.background_image = Some(url);
|
||||||
|
st.background_gradient = None;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
"color" => {
|
"color" => {
|
||||||
if let Some(c) = parse_color(v) {
|
if let Some(c) = parse_color(v) {
|
||||||
@@ -1121,6 +1237,19 @@ fn parse_traccia_singola(token: &str, font_size: f32) -> Option<Traccia> {
|
|||||||
parse_len(t, font_size).map(|l| Traccia::Px(l.resolve(font_size)))
|
parse_len(t, font_size).map(|l| Traccia::Px(l.resolve(font_size)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Estrae l'indirizzo da un `background(-image)` con `url(...)`.
|
||||||
|
fn url_di_background(valore: &str) -> Option<String> {
|
||||||
|
let inizio = valore.find("url(")? + 4;
|
||||||
|
let resto = &valore[inizio..];
|
||||||
|
let fine = resto.find(')')?;
|
||||||
|
let pulito = resto[..fine].trim().trim_matches(|c| c == '"' || c == '\'');
|
||||||
|
if pulito.is_empty() {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(pulito.to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn parse_len(value: &str, em: f32) -> Option<Len> {
|
pub fn parse_len(value: &str, em: f32) -> Option<Len> {
|
||||||
let v = value.trim();
|
let v = value.trim();
|
||||||
if let Some(px) = v.strip_suffix("px") {
|
if let Some(px) = v.strip_suffix("px") {
|
||||||
@@ -1237,7 +1366,9 @@ pub fn selector_matches(doc: &Document, node: NodeId, sel: &Selector) -> bool {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
let mut ancestor = doc.node(node).parent;
|
let mut ancestor = doc.node(node).parent;
|
||||||
for comp in sel.compounds.iter().rev().skip(1) {
|
for (i, comp) in sel.compounds.iter().enumerate().rev().skip(1) {
|
||||||
|
// `A > B` vuole il genitore diretto; `A B` un antenato qualsiasi
|
||||||
|
let diretto = sel.combinatori.get(i).copied() == Some('>');
|
||||||
let mut found = false;
|
let mut found = false;
|
||||||
while let Some(a) = ancestor {
|
while let Some(a) = ancestor {
|
||||||
if compound_matches(doc, a, comp) {
|
if compound_matches(doc, a, comp) {
|
||||||
@@ -1245,6 +1376,9 @@ pub fn selector_matches(doc: &Document, node: NodeId, sel: &Selector) -> bool {
|
|||||||
found = true;
|
found = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
if diretto {
|
||||||
|
break;
|
||||||
|
}
|
||||||
ancestor = doc.node(a).parent;
|
ancestor = doc.node(a).parent;
|
||||||
}
|
}
|
||||||
if !found {
|
if !found {
|
||||||
@@ -1276,9 +1410,53 @@ fn compound_matches(doc: &Document, node: NodeId, comp: &crate::css::Compound) -
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
for pseudo in &comp.pseudo {
|
||||||
|
if !pseudo_matches(doc, node, pseudo) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Corrispondenza delle pseudo-classi riconosciute (vedi `css::Pseudo`).
|
||||||
|
fn pseudo_matches(doc: &Document, node: NodeId, pseudo: &crate::css::Pseudo) -> bool {
|
||||||
|
use crate::css::Pseudo;
|
||||||
|
match pseudo {
|
||||||
|
Pseudo::Root => {
|
||||||
|
// `:root` è l'elemento html, quello senza genitore elemento
|
||||||
|
doc.element_name(node) == Some("html")
|
||||||
|
&& doc
|
||||||
|
.node(node)
|
||||||
|
.parent
|
||||||
|
.map(|p| doc.element_name(p).is_none())
|
||||||
|
.unwrap_or(true)
|
||||||
|
}
|
||||||
|
Pseudo::FirstChild => fratelli(doc, node).first().copied() == Some(node),
|
||||||
|
Pseudo::LastChild => fratelli(doc, node).last().copied() == Some(node),
|
||||||
|
Pseudo::Empty => doc.node(node).children.is_empty(),
|
||||||
|
Pseudo::Link => {
|
||||||
|
matches!(doc.element_name(node), Some("a") | Some("area"))
|
||||||
|
&& doc.attr(node, "href").is_some()
|
||||||
|
}
|
||||||
|
Pseudo::MaiAttiva => false,
|
||||||
|
Pseudo::Not(elenco) => !elenco.iter().any(|s| selector_matches(doc, node, s)),
|
||||||
|
Pseudo::Is(elenco, _) => elenco.iter().any(|s| selector_matches(doc, node, s)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Figli elemento dello stesso genitore (per `:first-child`/`:last-child`).
|
||||||
|
fn fratelli(doc: &Document, node: NodeId) -> Vec<NodeId> {
|
||||||
|
let Some(padre) = doc.node(node).parent else {
|
||||||
|
return vec![node];
|
||||||
|
};
|
||||||
|
doc.node(padre)
|
||||||
|
.children
|
||||||
|
.iter()
|
||||||
|
.copied()
|
||||||
|
.filter(|c| doc.element_name(*c).is_some())
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
/// Parole chiave di famiglia: non sono font installati, sono categorie. Il
|
/// Parole chiave di famiglia: non sono font installati, sono categorie. Il
|
||||||
/// motore ha una faccia sola per famiglia logica: se ne resta una generica,
|
/// motore ha una faccia sola per famiglia logica: se ne resta una generica,
|
||||||
/// si usa il font di base invece di cercare un file che si chiama "system-ui".
|
/// si usa il font di base invece di cercare un file che si chiama "system-ui".
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
Name: harbour-mosaic
|
Name: harbour-mosaic
|
||||||
Summary: Mosaic browser with an independent rendering engine
|
Summary: Mosaic browser with an independent rendering engine
|
||||||
Version: 0.8.14
|
Version: 0.8.15
|
||||||
Release: 1
|
Release: 1
|
||||||
Group: Qt/Qt
|
Group: Qt/Qt
|
||||||
License: MIT
|
License: MIT
|
||||||
@@ -21,6 +21,17 @@ engine written from scratch (no WebKit, Blink, Gecko or Servo code).
|
|||||||
Milestone M0: HTML parsing, CSS cascade, block layout and CPU raster.
|
Milestone M0: HTML parsing, CSS cascade, block layout and CPU raster.
|
||||||
|
|
||||||
%changelog
|
%changelog
|
||||||
|
* Thu Nov 12 2026 Carlo Baratto <carlo@carlobaratto.it> - 0.8.15-1
|
||||||
|
- CSS: font-style (corsivo sintetico), text-decoration, text-transform,
|
||||||
|
letter-spacing, opacity, border-radius, min/max-height, min-width,
|
||||||
|
overflow:hidden, background-image, linear-gradient
|
||||||
|
- Regole UA per link, em/i, b/strong, s/del; sfondo di html/body propagato alla tela
|
||||||
|
- Selettori: :where(), :is(), :not(), :root, :first-child, :last-child, :empty,
|
||||||
|
:link e il combinatore >. Senza :where() meta' del CSS dei temi WordPress
|
||||||
|
veniva scartato: era il motivo per cui i link restavano blu di default
|
||||||
|
- Corretto: la divisione dei selettori ignorava le parentesi, quindi :is(.a,.b)
|
||||||
|
si spezzava e la regola andava persa
|
||||||
|
|
||||||
* Thu Nov 12 2026 Carlo Baratto <carlo@carlobaratto.it> - 0.8.14-1
|
* Thu Nov 12 2026 Carlo Baratto <carlo@carlobaratto.it> - 0.8.14-1
|
||||||
- Immagini: max-width applicato e height:auto che segue il rapporto. Era il
|
- Immagini: max-width applicato e height:auto che segue il rapporto. Era il
|
||||||
difetto per cui una copertina 1200x630 usciva da una colonna da 700: la regola
|
difetto per cui una copertina 1200x630 usciva da una colonna da 700: la regola
|
||||||
|
|||||||
Reference in new issue
Block a user