-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add SVG backend (paintToSvg, paintToSvgElements)
Fixes #5
- Loading branch information
Showing
4 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import * as flow from '../src/api-with-parse.js'; | ||
import fs from 'fs'; | ||
|
||
const doc = flow.parse(` | ||
<html style="background-color: #eee; text-align: center;"> | ||
<div style="line-height: 1; color: white;"> | ||
<div style="display: inline-block;" x-dropflow-log> | ||
<span style="float: left; padding: 3px; background-color: rgb(212, 35, 41);">n</span> | ||
<span style="float: left; padding: 3px; background-color: black;">p</span> | ||
<span style="float: left; padding: 3px; background-color: rgb(41, 124, 187);">r</span> | ||
</div> | ||
</div> | ||
<p> | ||
<strong>more from</strong> | ||
<span style="display: inline-block; padding: 5px 10px; background-color: #7598c9; color: white;">news</span> | ||
<span style="display: inline-block; padding: 5px 10px; background-color: #7598c9; color: white;">culture</span> | ||
<span style="display: inline-block; padding: 5px 10px; background-color: #7598c9; color: white;">music</span> | ||
</p> | ||
</div> | ||
`); | ||
|
||
await flow.loadNotoFonts(doc, {paint: false}); | ||
const block = flow.generate(doc); | ||
console.log(block.repr()); | ||
flow.layout(block, 600, 100); | ||
const svg = flow.paintToSvg(block); | ||
|
||
fs.writeFileSync(new URL('svg-1.svg', import.meta.url), svg); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import {ShapedItem} from './layout-text.js'; | ||
import {firstCascadeItem} from './text-font.js'; | ||
|
||
import type {Color} from './style.js'; | ||
import type {PaintBackend} from './paint.js'; | ||
import type {FaceMatch} from './text-font.js'; | ||
|
||
function encode(s: string) { | ||
return s.replaceAll('&', '&').replaceAll('<', '<'); | ||
} | ||
|
||
function camelToKebab(camel: string) { | ||
return camel.replace(/[A-Z]/g, s => '-' + s.toLowerCase()); | ||
} | ||
|
||
export default class HtmlPaintBackend implements PaintBackend { | ||
s: string; | ||
fillColor: Color; | ||
strokeColor: Color; | ||
lineWidth: number; | ||
direction: 'ltr' | 'rtl'; | ||
font: FaceMatch; | ||
fontSize: number; | ||
usedFonts: Map<string, FaceMatch>; | ||
|
||
constructor() { | ||
this.s = ''; | ||
this.fillColor = {r: 0, g: 0, b: 0, a: 0}; | ||
this.strokeColor = {r: 0, g: 0, b: 0, a: 0}; | ||
this.lineWidth = 0; | ||
this.direction = 'ltr'; | ||
this.font = firstCascadeItem(); | ||
this.fontSize = 0; | ||
this.usedFonts = new Map(); | ||
} | ||
|
||
style(style: Record<string, string>) { | ||
return Object.entries(style).map(([prop, value]) => { | ||
return `${camelToKebab(prop)}: ${value}`; | ||
}).join('; '); | ||
} | ||
|
||
edge(x: number, y: number, length: number, side: 'top' | 'right' | 'bottom' | 'left') { | ||
const {r, g, b, a} = this.strokeColor; | ||
const sw = this.lineWidth; | ||
const width = side === 'top' || side === 'bottom' ? length + 'px' : sw + 'px'; | ||
const height = side === 'left' || side === 'right' ? length + 'px' : sw + 'px'; | ||
const backgroundColor = `rgba(${r}, ${g}, ${b}, ${a})`; | ||
|
||
this.s += `<rect x="${x}" y="${y}" width="${width}" height="${height}" fill="${backgroundColor}" />`; | ||
} | ||
|
||
text(x: number, y: number, item: ShapedItem, textStart: number, textEnd: number) { | ||
const text = item.paragraph.string.slice(textStart, textEnd).trim(); | ||
const {r, g, b, a} = this.fillColor; | ||
const color = `rgba(${r}, ${g}, ${b}, ${a})`; | ||
const style = this.style({ | ||
font: this.font.toFontString(this.fontSize), | ||
whiteSpace: 'pre', | ||
direction: this.direction, | ||
unicodeBidi: 'bidi-override' | ||
}); | ||
|
||
this.s += `<text x="${x}" y="${y}" style="${encode(style)}" fill="${color}">${encode(text)}</text>`; | ||
this.usedFonts.set(item.match.filename, item.match); | ||
} | ||
|
||
rect(x: number, y: number, w: number, h: number) { | ||
const {r, g, b, a} = this.fillColor; | ||
const fill = `rgba(${r}, ${g}, ${b}, ${a})`; | ||
|
||
this.s += `<rect x="${x}" y="${y}" width="${w}" height="${h}" fill="${fill}" />`; | ||
} | ||
} |