satori 0.0.0 → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,132 @@
1
+ <h1 align="center">
2
+ Satori
3
+ </h1>
4
+
5
+ ## Quick Start
6
+
7
+ `satori` is a function that takes a JSX element and returns a SVG string:
8
+
9
+ ```jsx
10
+ satori(
11
+ <div style={{ color: 'black' }}>hello, world</div>,
12
+ {
13
+ width: 600,
14
+ height: 400,
15
+ fonts: [
16
+ {
17
+ name: 'Roboto',
18
+ data: robotoArrayBuffer,
19
+ weight: 400,
20
+ style: 'normal',
21
+ },
22
+ ...
23
+ ],
24
+ }
25
+ )
26
+ ```
27
+
28
+ Which yields:
29
+
30
+ ```js
31
+ '<svg ...><text x="0" y="16" width="45" height="16" fill="black">hello, world</text></svg>'
32
+ ```
33
+
34
+ ## Playground
35
+
36
+ (TBD)
37
+
38
+ ## Documentation
39
+
40
+ ### JSX
41
+
42
+ (TBD: Only pure and stateless function components, only inlined styles.)
43
+
44
+ ### HTML Elements
45
+
46
+ Satori supports a limited subset of HTML and CSS features, due to its special use cases. In general, only these static and visible elements and properties that are implemented.
47
+
48
+ For example, the `<input>` HTML element, the `cursor` CSS property are not in consideration.
49
+
50
+ (TBD)
51
+
52
+ ### CSS Properties
53
+
54
+ | Property | Supported Values |
55
+ | --- | --- |
56
+ | `display` | `none`, `flex` |
57
+ | `position` | `relative`, `absolute` |
58
+ | `margin`, `padding` | Supported |
59
+ | `top`, `right`, `bottom`, `left` | Supported |
60
+ | `width`, `height` | Supported |
61
+ | `max-width`, `max-height` | Supported |
62
+ | `min-width`, `min-height` | Supported |
63
+ | `border` | Supported |
64
+ | `flex-direction` | Supported |
65
+ | `flex-wrap` | Supported |
66
+ | `flex-grow` | Supported |
67
+ | `flex-shrink` | Supported |
68
+ | `flex-basis` | Supported |
69
+ | `align-items` | Supported |
70
+ | `align-content` | Supported |
71
+ | `align-self` | Supported |
72
+ | `justify-content` | Supported |
73
+ | `font-family` | Support single value |
74
+ | `font-size` | Supported |
75
+ | `font-weight` | Supported |
76
+ | `font-style` | Supported |
77
+ | `text-align` | Supported |
78
+ | `letter-spacing` | Supported |
79
+ | `box-shadow` | All supported except spread-radius (works like `drop-shadow`) |
80
+ | `border-radius` | Supported |
81
+ | `overflow` | `visible`, `hidden` |
82
+ | `color` | Supported |
83
+ | `transform` | Support absolute values |
84
+ | `object-fit` | `contain`, `cover`, `none` |
85
+ | `opacity` | Supported |
86
+ | `background-color` | Supported |
87
+ | `background-image` | Support `linear-gradient`, `url` |
88
+ | `background-clip` | TBD |
89
+ | `background-size` | TBD |
90
+ | `background-position` | TBD |
91
+ | `background-repeat` | TBD |
92
+ | `background-origin` | TBD |
93
+ | `text-decoration` | TBD |
94
+ | `text-shadow` | TBD |
95
+ | `text-transform` | TBD |
96
+ | `word-break` | TBD |
97
+ | `transform-origin` | TBD |
98
+
99
+ Note:
100
+
101
+ 1. Three-dimensional transforms are not supported.
102
+ 2. There is no `z-index` support in SVG. Elements that come later in the document will be drawn on top.
103
+
104
+ ## Contribute
105
+
106
+ This project uses [pnpm](https://pnpm.io). To install dependencies, run:
107
+
108
+ ```bash
109
+ pnpm install
110
+ ```
111
+
112
+ To start the preview mode, run:
113
+
114
+ ```bash
115
+ pnpm dev:preview
116
+ ```
117
+
118
+ To start and live-watch the tests, run:
119
+
120
+ ```bash
121
+ pnpm dev:test
122
+ ```
123
+
124
+ ## Author
125
+
126
+ - Shu Ding ([@shuding_](https://twitter.com/shuding_))
127
+
128
+ ---
129
+
130
+ <a aria-label="Vercel logo" href="https://vercel.com">
131
+ <img src="https://badgen.net/badge/icon/Made%20by%20Vercel?icon=zeit&label&color=black&labelColor=black">
132
+ </a>
@@ -0,0 +1,23 @@
1
+ import { ReactNode } from 'react';
2
+
3
+ /**
4
+ * This class handles everything related to fonts.
5
+ */
6
+
7
+ declare type Weight = 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900;
8
+ declare type Style = 'normal' | 'italic';
9
+ interface FontOptions {
10
+ data: Buffer | ArrayBuffer;
11
+ name: string;
12
+ weight?: Weight;
13
+ style?: Style;
14
+ }
15
+
16
+ interface SatoriOptions {
17
+ width: number;
18
+ height: number;
19
+ fonts: FontOptions[];
20
+ }
21
+ declare function satori(element: ReactNode, options: SatoriOptions): string;
22
+
23
+ export { SatoriOptions, satori as default };
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ var dt=Object.create;var I=Object.defineProperty;var gt=Object.getOwnPropertyDescriptor;var ht=Object.getOwnPropertyNames,et=Object.getOwnPropertySymbols,bt=Object.getPrototypeOf,nt=Object.prototype.hasOwnProperty,yt=Object.prototype.propertyIsEnumerable;var rt=(t,r,e)=>r in t?I(t,r,{enumerable:!0,configurable:!0,writable:!0,value:e}):t[r]=e,P=(t,r)=>{for(var e in r||(r={}))nt.call(r,e)&&rt(t,e,r[e]);if(et)for(var e of et(r))yt.call(r,e)&&rt(t,e,r[e]);return t};var ot=t=>I(t,"__esModule",{value:!0});var Et=(t,r)=>{for(var e in r)I(t,e,{get:r[e],enumerable:!0})},it=(t,r,e,o)=>{if(r&&typeof r=="object"||typeof r=="function")for(let s of ht(r))!nt.call(t,s)&&(e||s!=="default")&&I(t,s,{get:()=>r[s],enumerable:!(o=gt(r,s))||o.enumerable});return t},$=(t,r)=>it(ot(I(t!=null?dt(bt(t)):{},"default",!r&&t&&t.__esModule?{get:()=>t.default,enumerable:!0}:{value:t,enumerable:!0})),t),_t=(t=>(r,e)=>t&&t.get(r)||(e=it(ot({}),r,1),t&&t.set(r,e),e))(typeof WeakMap!="undefined"?new WeakMap:0);var St={};Et(St,{default:()=>Q});var O=$(require("yoga-layout-prebuilt"));var pt=$(require("yoga-layout-prebuilt"));function w(t){let r=typeof t;return!(r==="number"||r==="bigint"||r==="string"||r==="boolean")}function at(t){return/^class\s/.test(Function.prototype.toString.call(t))}function T(t,r){return[t[0]*r[0]+t[2]*r[1],t[1]*r[0]+t[3]*r[1],t[0]*r[2]+t[2]*r[3],t[1]*r[2]+t[3]*r[3],t[0]*r[4]+t[2]*r[5]+t[4],t[1]*r[4]+t[3]*r[5]+t[5]]}var a=$(require("yoga-layout-prebuilt"));var st={p:{display:"block",marginTop:"1em",marginBottom:"1em"},div:{display:"block"},blockquote:{display:"block",marginTop:"1em",marginBottom:"1em",marginLeft:40,marginRight:40},center:{display:"block",textAlign:"center"},hr:{display:"block",marginTop:"0.5em",marginBottom:"0.5em",marginLeft:"auto",marginRight:"auto",borderWidth:1,borderStyle:"inset"},h1:{display:"block",fontSize:"2em",marginTop:"0.67em",marginBottom:"0.67em",marginLeft:0,marginRight:0,fontWeight:"bold"},h2:{display:"block",fontSize:"1.5em",marginTop:"0.83em",marginBottom:"0.83em",marginLeft:0,marginRight:0,fontWeight:"bold"},h3:{display:"block",fontSize:"1.17em",marginTop:"1em",marginBottom:"1em",marginLeft:0,marginRight:0,fontWeight:"bold"},h4:{display:"block",marginTop:"1.33em",marginBottom:"1.33em",marginLeft:0,marginRight:0,fontWeight:"bold"},h5:{display:"block",fontSize:"0.83em",marginTop:"1.67em",marginBottom:"1.67em",marginLeft:0,marginRight:0,fontWeight:"bold"},h6:{display:"block",fontSize:"0.67em",marginTop:"2.33em",marginBottom:"2.33em",marginLeft:0,marginRight:0,fontWeight:"bold"},u:{textDecoration:"underline"},strong:{fontWeight:"bold"},b:{fontWeight:"bold"},i:{fontStyle:"italic"},em:{fontStyle:"italic"},code:{fontFamily:"monospace"},kbd:{fontFamily:"monospace"},pre:{display:"block",fontFamily:"monospace",whiteSpace:"pre",marginTop:"1em",marginBottom:"1em"},mark:{backgroundColor:"yellow",color:"black"},big:{fontSize:"larger"},small:{fontSize:"smaller"},s:{textDecoration:"line-through"}};var $t=new Set(["color","font","fontFamily","fontSize","fontStyle","fontWeight","lineHeight","textAlign","textTransform","whiteSpace","letterSpacing","transform","opacity"]);function Y(t){let r={};for(let e in t)$t.has(e)&&(r[e]=t[e]);return r}var B=$(require("css-to-react-native")),X=$(require("parse-css-dimension")),lt=require("css-background-parser");var xt=new Set(["flex","flexGrow","flexShrink","flexBasis","fontWeight","lineHeight","opacity","scale","scaleX","scaleY"]),ft=[1,0,0,1,0,0];function Rt(t,r){return typeof r=="number"?xt.has(t)?String(r):r+"px":r}function ut(t,r){if(typeof t=="number")return t;try{let e=new X.default(t);if(e.type==="length")switch(e.unit){case"em":return e.value*r;case"rem":return e.value*16;default:return e.value}else if(e.type==="angle")switch(e.unit){case"deg":return e.value;case"rad":return e.value*180/Math.PI;default:return e.value}}catch{}}function v(t,r){let e=[];for(let n in t){let i=(0,B.getPropertyName)(n);e.push([i,Rt(i,t[n])])}let o=(0,B.default)(e);if(o.backgroundImage){let{backgrounds:n}=(0,lt.parseElementStyle)(o);o.backgroundImage=n}let s=o.fontSize||r.fontSize;if(typeof s=="string")try{let n=new X.default(s);switch(n.unit){case"em":s=n.value*r.fontSize;break;case"rem":s=n.value*16;break}}catch{s=16}o.fontSize=s;for(let n in o){let i=o[n];if(typeof i=="string"){let f=ut(i,s);typeof f!="undefined"&&(o[n]=f),i=o[n]}if(n==="opacity"&&(i=o[n]=i*r.opacity),n==="transform"){let f=[...ft],c=i;for(let l of c){let d=Object.keys(l)[0],p=l[d],m=typeof p=="string"?ut(p,s):p,u=[...ft];switch(d){case"translateX":u[4]=m;break;case"translateY":u[5]=m;break;case"scaleX":u[0]=m;break;case"scaleY":u[3]=m;break;case"rotate":let g=m*Math.PI/180,h=Math.cos(g),y=Math.sin(g);u[0]=h,u[1]=y,u[2]=-y,u[3]=h;break;case"skewX":u[2]=Math.tan(m*Math.PI/180);break;case"skewY":u[1]=Math.tan(m*Math.PI/180);break}f=T(f,u)}o.transform=f}}return o}function x(t,r,e){let o=r[t];return typeof o=="undefined"?e:o}function H(t,r,e,o,s){let n=P(P(P({},e),v(st[r],e)),v(o,e));if(r==="img"){let i=parseInt(s.width),c=parseInt(s.height)/i;n.width||(n.width=i),n.height||(n.height=c*n.width)}return t.setDisplay(x(n.display,{flex:a.default.DISPLAY_FLEX,none:a.default.DISPLAY_NONE},a.default.DISPLAY_FLEX)),t.setAlignContent(x(n.alignContent,{stretch:a.default.ALIGN_STRETCH,center:a.default.ALIGN_CENTER,"flex-start":a.default.ALIGN_FLEX_START,"flex-end":a.default.ALIGN_FLEX_END,"space-between":a.default.ALIGN_SPACE_BETWEEN,"space-around":a.default.ALIGN_SPACE_AROUND,baseline:a.default.ALIGN_BASELINE,normal:a.default.ALIGN_AUTO},a.default.ALIGN_AUTO)),t.setAlignItems(x(n.alignItems,{stretch:a.default.ALIGN_STRETCH,center:a.default.ALIGN_CENTER,"flex-start":a.default.ALIGN_FLEX_START,"flex-end":a.default.ALIGN_FLEX_END,baseline:a.default.ALIGN_BASELINE,normal:a.default.ALIGN_AUTO},a.default.ALIGN_FLEX_START)),t.setAlignSelf(x(n.alignSelf,{stretch:a.default.ALIGN_STRETCH,center:a.default.ALIGN_CENTER,"flex-start":a.default.ALIGN_FLEX_START,"flex-end":a.default.ALIGN_FLEX_END,baseline:a.default.ALIGN_BASELINE,normal:a.default.ALIGN_AUTO},a.default.ALIGN_AUTO)),t.setJustifyContent(x(n.justifyContent,{center:a.default.JUSTIFY_CENTER,"flex-start":a.default.JUSTIFY_FLEX_START,"flex-end":a.default.JUSTIFY_FLEX_END,"space-between":a.default.JUSTIFY_SPACE_BETWEEN,"space-around":a.default.JUSTIFY_SPACE_AROUND},a.default.JUSTIFY_FLEX_START)),typeof n.flexBasis!="undefined"?t.setFlexBasis(n.flexBasis):n.display==="block"&&(typeof n.width=="undefined"||n.width==="auto")&&(typeof n.maxWidth=="undefined"||n.maxWidth==="auto")&&t.setFlexBasisPercent(100),t.setFlexDirection(x(n.flexDirection,{row:a.default.FLEX_DIRECTION_ROW,column:a.default.FLEX_DIRECTION_COLUMN,"row-reverse":a.default.FLEX_DIRECTION_ROW_REVERSE,"column-reverse":a.default.FLEX_DIRECTION_COLUMN_REVERSE},a.default.FLEX_DIRECTION_ROW)),t.setFlexGrow(n.flexGrow||0),t.setFlexShrink(n.flexShrink||1),t.setFlexWrap(x(n.flexWrap,{wrap:a.default.WRAP_WRAP,nowrap:a.default.WRAP_NO_WRAP,"wrap-reverse":a.default.WRAP_WRAP_REVERSE},a.default.WRAP_WRAP)),typeof n.maxHeight!="undefined"&&t.setMaxHeight(n.maxHeight),typeof n.maxWidth!="undefined"&&t.setMaxWidth(n.maxWidth),typeof n.minHeight!="undefined"&&t.setMinHeight(n.minHeight),typeof n.minWidth!="undefined"&&t.setMinWidth(n.minWidth),t.setOverflow(x(n.overflow,{visible:a.default.OVERFLOW_VISIBLE,hidden:a.default.OVERFLOW_HIDDEN},a.default.OVERFLOW_VISIBLE)),t.setMargin(a.default.EDGE_TOP,n.marginTop||0),t.setMargin(a.default.EDGE_BOTTOM,n.marginBottom||0),t.setMargin(a.default.EDGE_LEFT,n.marginLeft||0),t.setMargin(a.default.EDGE_RIGHT,n.marginRight||0),t.setBorder(a.default.EDGE_TOP,n.borderWidth||0),t.setBorder(a.default.EDGE_BOTTOM,n.borderWidth||0),t.setBorder(a.default.EDGE_LEFT,n.borderWidth||0),t.setBorder(a.default.EDGE_RIGHT,n.borderWidth||0),t.setPadding(a.default.EDGE_TOP,n.paddingTop||0),t.setPadding(a.default.EDGE_BOTTOM,n.paddingBottom||0),t.setPadding(a.default.EDGE_LEFT,n.paddingLeft||0),t.setPadding(a.default.EDGE_RIGHT,n.paddingRight||0),t.setPositionType((n.position,a.default.POSITION_TYPE_ABSOLUTE,a.default.POSITION_TYPE_RELATIVE,a.default.POSITION_TYPE_RELATIVE)),typeof n.top!="undefined"&&t.setPosition(a.default.EDGE_TOP,n.top),typeof n.bottom!="undefined"&&t.setPosition(a.default.EDGE_BOTTOM,n.bottom),typeof n.left!="undefined"&&t.setPosition(a.default.EDGE_LEFT,n.left),typeof n.right!="undefined"&&t.setPosition(a.default.EDGE_RIGHT,n.right),typeof n.height!="undefined"?t.setHeight(n.height):t.setHeightAuto(),typeof n.width!="undefined"?t.setWidth(n.width):t.setWidthAuto(),[n,Y(n)]}var R=$(require("yoga-layout-prebuilt")),ct=require("css-line-break");function L({left:t,top:r,width:e,height:o},s,n){let i;if(n)i=s;else{let f=t+e/2,c=r+o/2;i=T([1,0,0,1,f,c],T(s,[1,0,0,1,-f,-c])),s.__parent&&(i=T(s.__parent,i)),s.splice(0,6,...i)}return`matrix(${i.join(",")})`}function U({left:t,top:r,width:e,height:o,content:s,isInheritingTransform:n},i){let f="",c=1;return i.transform&&(f=L({left:t,top:r,width:e,height:o},i.transform,n)),i.opacity&&(c=+i.opacity),`<text x="${t}" y="${r+o}" width="${e}" height="${o}" fill="${i.color}" font-weight="${i.fontWeight}" font-style="${i.fontStyle}" font-size="${i.fontSize}" font-family="${i.fontFamily}" ${i.letterSpacing?`letter-spacing="${i.letterSpacing}"`:""} ${f?`transform="${f}"`:""} ${c!==1?`opacity="${c}"`:""}>${s}</text>`}function*z(t,r){let{parentStyle:e,parent:o,font:s,id:n,isInheritingTransform:i}=r,f=(0,ct.LineBreaker)(t,{lineBreak:"strict",wordBreak:"normal"}),c=[];for(let u;!(u=f.next()).done;)c.push(u.value.slice());let l=[];o.setAlignItems(R.default.ALIGN_BASELINE),e.textAlign==="left"?o.setJustifyContent(R.default.JUSTIFY_FLEX_START):e.textAlign==="center"?o.setJustifyContent(R.default.JUSTIFY_CENTER):e.textAlign==="right"?o.setJustifyContent(R.default.JUSTIFY_FLEX_END):e.textAlign==="justify"&&o.setJustifyContent(R.default.JUSTIFY_SPACE_BETWEEN);for(let u of c){let g=R.default.Node.create();o.insertChild(g,o.getChildCount());let h=s.measure(u,e),y=(e.letterSpacing||0)*u.length;g.setWidth(h.width+y),g.setHeight(h.ascent*1.2),g.setMargin(R.default.EDGE_BOTTOM,h.descent*1.2),l.push(g)}let[d,p]=yield,m="";for(let u=0;u<l.length;u++){let g=l[u];e.position==="absolute"&&g.calculateLayout();let{left:h,top:y,width:E,height:S}=g.getComputedLayout();h+=d,y+=p,m+=U({id:n,left:h,top:y,width:E,height:S,content:c[u],isInheritingTransform:i},e)}return m}var mt=$(require("gradient-parser"));function Tt(t){return t.type==="literal"?t.value:t.type==="hex"?`#${t.value}`:t.type==="rgb"?`rgb(${t.value.join(",")})`:t.type==="rgba"?`rgba(${t.value.join(",")})`:"transparent"}function J({id:t,width:r},{image:e}){if(e.startsWith("linear-gradient(")){let o=mt.default.parse(e)[0],s,n,i,f;if(o.orientation.type==="directional")[s,n,i,f]={top:[0,1,0,0],bottom:[0,0,0,1],left:[1,0,0,0],right:[0,0,1,0]}[o.orientation.value];else if(o.orientation.type==="angular"){let u=+o.orientation.value/180*Math.PI-Math.PI/2,g=Math.cos(u),h=Math.sin(u);s=0,n=0,i=g,f=h,i<0&&(s-=i,i=0),f<0&&(n-=f,f=0)}let c=r,l=[];for(let u of o.colorStops){let g=Tt(u);if(!l.length&&(l.push({offset:0,color:g}),typeof u.length=="undefined"||u.length.value==="0"))continue;let h=typeof u.length=="undefined"?void 0:u.length.type==="%"?u.length.value/100:u.length.value/c;l.push({offset:h,color:g})}l.length||l.push({offset:0,color:"transparent"});let d=l[l.length-1];d.offset!==1&&(typeof d.offset=="undefined"?d.offset=1:l.push({offset:1,color:d.color}));let p=0,m=1;for(let u=0;u<l.length;u++)if(typeof l[u].offset=="undefined"){for(m<u&&(m=u);typeof l[m].offset=="undefined";)m++;l[u].offset=(l[m].offset-l[p].offset)/(m-p)*(u-p)+l[p].offset}else p=u;return[`satori_bi${t}`,`<linearGradient id="satori_bi${t}" x1="${s}" y1="${n}" x2="${i}" y2="${f}">${l.map(u=>`<stop offset="${u.offset*100}%" stop-color="${u.color}"/>`).join("")}</linearGradient>`]}if(e.startsWith("url(")){let o=e.slice(4,-1);return[`satori_bi${t}`,`<pattern id="satori_bi${t}" patternContentUnits="objectBoundingBox" width="1" height="1"><image href="${o}" x="0" y="0" width="1" height="1"/></pattern>`]}}function D(t,r,e){return e<t+r&&(e/2<t&&e/2<r?t=r=e/2:e/2<t?t=e-r:e/2<r&&(r=e-t)),[t,r]}function A({left:t,top:r,width:e,height:o},s){let{borderTopLeftRadius:n,borderTopRightRadius:i,borderBottomLeftRadius:f,borderBottomRightRadius:c}=s;return n=Math.min(n||0,e,o),i=Math.min(i||0,e,o),f=Math.min(f||0,e,o),c=Math.min(c||0,e,o),!n&&!i&&!f&&!c?"":([n,i]=D(n,i,e),[n,f]=D(n,f,o),[i,c]=D(i,c,o),[f,c]=D(f,c,e),`M${t+n},${r} h${e-n-i} a${i},${i} 0 0 1 ${i},${i} v${o-i-c} a${c},${c} 0 0 1 ${-c},${c} h${c+f-e} a${f},${f} 0 0 1 ${-f},${-f} v${f+n-o} a${n},${n} 0 0 1 ${n},${-n}`)}function N({id:t,width:r,height:e},o){if(!o.shadowColor||!o.shadowOffset||typeof o.shadowRadius=="undefined")return"";let s=Math.min(o.shadowOffset.width-o.shadowRadius*2,0),n=Math.max(o.shadowOffset.width+o.shadowRadius*2+r,r),i=Math.min(o.shadowOffset.height-o.shadowRadius*2,0),f=Math.max(o.shadowOffset.height+o.shadowRadius*2+e,e);return`<defs><filter id="satori_s-${t}" x="${s/r*100}%" y="${i/e*100}%" width="${(n-s)/r*100}%" height="${(f-i)/e*100}%"><feDropShadow dx="${o.shadowOffset.width}" dy="${o.shadowOffset.height}" stdDeviation="${o.shadowRadius}" flood-color="${o.shadowColor}" flood-opacity="1"/></filter></defs>`}function j({id:t,left:r,top:e,width:o,height:s,isInheritingTransform:n},i){if(i.display==="none")return"";let f="rect",c="transparent",l=0,d="",p="",m=[],u=1;if(i.backgroundColor&&m.push(i.backgroundColor),i.borderWidth&&(l=i.borderWidth,c=i.borderColor),i.opacity&&(u=+i.opacity),i.transform&&(d=L({left:r,top:e,width:o,height:s},i.transform,n)),i.backgroundImage){let y=i.backgroundImage.map((E,S)=>J({id:t+"_"+S,width:o,height:s},E)).filter(Boolean);for(let E of y)p+=E[1],m.push(`url(#${E[0]})`)}let g=A({left:r,top:e,width:o,height:s},i);g&&(f="path");let h=N({width:o,height:s,id:t},i);return m.length||m.push("transparent"),`${p?`<defs>${p}</defs>`:""}${h?`${h}<g filter="url(#satori_s-${t})">`:""}${u!==1?`<g opacity="${u}">`:""}${m.map((y,E)=>y==="transparent"&&!(E===m.length-1&&l)?"":`<${f} x="${r}" y="${e}" width="${o}" height="${s}" fill="${y}" ${E===m.length-1&&l?`stroke="${c}" stroke-width="${l}"`:""} ${g?`d="${g}"`:""} ${d?`transform="${d}"`:""}></${f}>`).join("")}${u!==1?"</g>":""}${h?"</g>":""}`}function V({id:t,left:r,top:e,width:o,height:s,src:n},i){if(i.display==="none")return"";let f="",c=1,l=i.objectFit==="contain"?"xMidYMid":i.objectFit==="cover"?"xMidYMid slice":"none",d=A({left:r,top:e,width:o,height:s},i);d&&(f=`<clipPath id="satori_c-${t}"><path x="${r}" y="${e}" width="${o}" height="${s}" d="${d}"></path></clipPath>`),i.opacity&&(c=+i.opacity);let p=N({width:o,height:s,id:t},i);return`${p}${p?`<g filter="url(#satori_s-${t})">`:""}${f}<image href="${n}" x="${r}" y="${e}" width="${o}" height="${s}" preserveAspectRatio="${l}" ${f?`clip-path="url(#satori_c-${t})"`:""} ${c!==1?`opacity="${c}"`:""}></image>${p?"</g>":""}`}function*F(t,r){let{id:e,inheritedStyle:o,parent:s,font:n}=r;if(t===null||typeof t=="undefined")return yield,"";if(!w(t)||typeof t.type=="function"){let _;if(!w(t))_=z(String(t),r);else{if(at(t.type))throw new Error("Class component is not supported.");_=F(t.type(t.props),r)}_.next();let G=yield;return _.next(G).value}let{type:i,props:f}=t,{style:c,children:l}=f,d=pt.default.Node.create();s.insertChild(d,s.getChildCount());let[p,m]=H(d,i,o,c,f),u=p.transform===o.transform;u||(p.transform.__parent=o.transform);let g=typeof l=="undefined"?[]:[].concat(l),h=[],y=0;for(let _ of g){let G=F(_,{id:e*g.length+ ++y,parentStyle:p,inheritedStyle:m,isInheritingTransform:!0,parent:d,font:n});G.next(),h.push(G)}let[E,S]=yield;p.position==="absolute"&&d.calculateLayout();let{left:W,top:k,width:Z,height:tt}=d.getComputedLayout();W+=E,k+=S;let C="";i==="img"?C=V({id:e,left:W,top:k,width:Z,height:tt,src:f.src,isInheritingTransform:u},p):C=j({id:e,left:W,top:k,width:Z,height:tt,isInheritingTransform:u},p);for(let _ of h)C+=_.next([W,k]).value;return C}var q=$(require("opentype.js")),M=class{constructor(r){this.fonts=new Map;for(let e of r){let o=e.data,s="buffer"in o?q.default.parse(o.buffer.slice(o.byteOffset,o.byteOffset+o.byteLength)):q.default.parse(o);this.defaultFont||(this.defaultFont=s),this.fonts.has(e.name)||this.fonts.set(e.name,[]),this.fonts.get(e.name).push([s,e.weight,e.style])}}get({name:r,weight:e,style:o}){if(!this.fonts.has(r))return this.defaultFont;e==="normal"&&(e=400),e==="bold"&&(e=700);let s=[...this.fonts.get(r)];return s.sort(([n,i,f],[c,l,d])=>{if(i!==l)return i?!l||i===e?-1:l===e?1:e===400&&i===500||e===500&&i===400?-1:e===400&&l===500||e===500&&l===400?1:e<400?i<e&&l<e?l-i:i<e?-1:l<e?1:i-l:e<i&&e<l?i-l:e<i?-1:e<l?1:l-i:1;if(f!==d){if(f===o)return-1;if(d===o)return 1}return-1}),s[0][0]}measure(r,{fontFamily:e,fontSize:o,fontWeight:s=400,fontStyle:n="normal"}){let i=this.get({name:e,weight:s,style:n});return{width:i.getAdvanceWidth(r,o),ascent:i.ascender/i.unitsPerEm*o,descent:-(i.descender/i.unitsPerEm)*o}}};function K({width:t,height:r,content:e},o){return`<svg width="${t}" height="${r}" viewBox="0 0 ${t} ${r}" xmlns="http://www.w3.org/2000/svg">${e}</svg>`}function Q(t,r){let e=new M(r.fonts),o=O.default.Node.create();o.setWidth(r.width),o.setHeight(r.height),o.setFlexDirection(O.default.FLEX_DIRECTION_ROW),o.setFlexWrap(O.default.WRAP_WRAP);let s=F(t,{id:1,parentStyle:{},inheritedStyle:{fontSize:16,fontWeight:"normal",fontFamily:"serif",fontStyle:"normal",lineHeight:1.2,color:"black",opacity:1},parent:o,font:e});s.next(),o.calculateLayout(r.width,r.height,O.default.DIRECTION_LTR);let n=s.next([0,0]).value;return K({width:r.width,height:r.height,content:n})}module.exports=_t(St);0&&(module.exports={});
package/dist/index.mjs ADDED
@@ -0,0 +1 @@
1
+ var it=Object.defineProperty;var q=Object.getOwnPropertySymbols;var at=Object.prototype.hasOwnProperty,st=Object.prototype.propertyIsEnumerable;var K=(t,o,e)=>o in t?it(t,o,{enumerable:!0,configurable:!0,writable:!0,value:e}):t[o]=e,C=(t,o)=>{for(var e in o||(o={}))at.call(o,e)&&K(t,e,o[e]);if(q)for(var e of q(o))st.call(o,e)&&K(t,e,o[e]);return t};import v from"yoga-layout-prebuilt";import bt from"yoga-layout-prebuilt";function D(t){let o=typeof t;return!(o==="number"||o==="bigint"||o==="string"||o==="boolean")}function Q(t){return/^class\s/.test(Function.prototype.toString.call(t))}function T(t,o){return[t[0]*o[0]+t[2]*o[1],t[1]*o[0]+t[3]*o[1],t[0]*o[2]+t[2]*o[3],t[1]*o[2]+t[3]*o[3],t[0]*o[4]+t[2]*o[5]+t[4],t[1]*o[4]+t[3]*o[5]+t[5]]}import a from"yoga-layout-prebuilt";var Z={p:{display:"block",marginTop:"1em",marginBottom:"1em"},div:{display:"block"},blockquote:{display:"block",marginTop:"1em",marginBottom:"1em",marginLeft:40,marginRight:40},center:{display:"block",textAlign:"center"},hr:{display:"block",marginTop:"0.5em",marginBottom:"0.5em",marginLeft:"auto",marginRight:"auto",borderWidth:1,borderStyle:"inset"},h1:{display:"block",fontSize:"2em",marginTop:"0.67em",marginBottom:"0.67em",marginLeft:0,marginRight:0,fontWeight:"bold"},h2:{display:"block",fontSize:"1.5em",marginTop:"0.83em",marginBottom:"0.83em",marginLeft:0,marginRight:0,fontWeight:"bold"},h3:{display:"block",fontSize:"1.17em",marginTop:"1em",marginBottom:"1em",marginLeft:0,marginRight:0,fontWeight:"bold"},h4:{display:"block",marginTop:"1.33em",marginBottom:"1.33em",marginLeft:0,marginRight:0,fontWeight:"bold"},h5:{display:"block",fontSize:"0.83em",marginTop:"1.67em",marginBottom:"1.67em",marginLeft:0,marginRight:0,fontWeight:"bold"},h6:{display:"block",fontSize:"0.67em",marginTop:"2.33em",marginBottom:"2.33em",marginLeft:0,marginRight:0,fontWeight:"bold"},u:{textDecoration:"underline"},strong:{fontWeight:"bold"},b:{fontWeight:"bold"},i:{fontStyle:"italic"},em:{fontStyle:"italic"},code:{fontFamily:"monospace"},kbd:{fontFamily:"monospace"},pre:{display:"block",fontFamily:"monospace",whiteSpace:"pre",marginTop:"1em",marginBottom:"1em"},mark:{backgroundColor:"yellow",color:"black"},big:{fontSize:"larger"},small:{fontSize:"smaller"},s:{textDecoration:"line-through"}};var ft=new Set(["color","font","fontFamily","fontSize","fontStyle","fontWeight","lineHeight","textAlign","textTransform","whiteSpace","letterSpacing","transform","opacity"]);function M(t){let o={};for(let e in t)ft.has(e)&&(o[e]=t[e]);return o}import ut,{getPropertyName as lt}from"css-to-react-native";import rt from"parse-css-dimension";import{parseElementStyle as ct}from"css-background-parser";var mt=new Set(["flex","flexGrow","flexShrink","flexBasis","fontWeight","lineHeight","opacity","scale","scaleX","scaleY"]),tt=[1,0,0,1,0,0];function pt(t,o){return typeof o=="number"?mt.has(t)?String(o):o+"px":o}function et(t,o){if(typeof t=="number")return t;try{let e=new rt(t);if(e.type==="length")switch(e.unit){case"em":return e.value*o;case"rem":return e.value*16;default:return e.value}else if(e.type==="angle")switch(e.unit){case"deg":return e.value;case"rad":return e.value*180/Math.PI;default:return e.value}}catch{}}function G(t,o){let e=[];for(let r in t){let n=lt(r);e.push([n,pt(n,t[r])])}let i=ut(e);if(i.backgroundImage){let{backgrounds:r}=ct(i);i.backgroundImage=r}let s=i.fontSize||o.fontSize;if(typeof s=="string")try{let r=new rt(s);switch(r.unit){case"em":s=r.value*o.fontSize;break;case"rem":s=r.value*16;break}}catch{s=16}i.fontSize=s;for(let r in i){let n=i[r];if(typeof n=="string"){let f=et(n,s);typeof f!="undefined"&&(i[r]=f),n=i[r]}if(r==="opacity"&&(n=i[r]=n*o.opacity),r==="transform"){let f=[...tt],c=n;for(let l of c){let d=Object.keys(l)[0],p=l[d],m=typeof p=="string"?et(p,s):p,u=[...tt];switch(d){case"translateX":u[4]=m;break;case"translateY":u[5]=m;break;case"scaleX":u[0]=m;break;case"scaleY":u[3]=m;break;case"rotate":let g=m*Math.PI/180,h=Math.cos(g),E=Math.sin(g);u[0]=h,u[1]=E,u[2]=-E,u[3]=h;break;case"skewX":u[2]=Math.tan(m*Math.PI/180);break;case"skewY":u[1]=Math.tan(m*Math.PI/180);break}f=T(f,u)}i.transform=f}}return i}function x(t,o,e){let i=o[t];return typeof i=="undefined"?e:i}function w(t,o,e,i,s){let r=C(C(C({},e),G(Z[o],e)),G(i,e));if(o==="img"){let n=parseInt(s.width),c=parseInt(s.height)/n;r.width||(r.width=n),r.height||(r.height=c*r.width)}return t.setDisplay(x(r.display,{flex:a.DISPLAY_FLEX,none:a.DISPLAY_NONE},a.DISPLAY_FLEX)),t.setAlignContent(x(r.alignContent,{stretch:a.ALIGN_STRETCH,center:a.ALIGN_CENTER,"flex-start":a.ALIGN_FLEX_START,"flex-end":a.ALIGN_FLEX_END,"space-between":a.ALIGN_SPACE_BETWEEN,"space-around":a.ALIGN_SPACE_AROUND,baseline:a.ALIGN_BASELINE,normal:a.ALIGN_AUTO},a.ALIGN_AUTO)),t.setAlignItems(x(r.alignItems,{stretch:a.ALIGN_STRETCH,center:a.ALIGN_CENTER,"flex-start":a.ALIGN_FLEX_START,"flex-end":a.ALIGN_FLEX_END,baseline:a.ALIGN_BASELINE,normal:a.ALIGN_AUTO},a.ALIGN_FLEX_START)),t.setAlignSelf(x(r.alignSelf,{stretch:a.ALIGN_STRETCH,center:a.ALIGN_CENTER,"flex-start":a.ALIGN_FLEX_START,"flex-end":a.ALIGN_FLEX_END,baseline:a.ALIGN_BASELINE,normal:a.ALIGN_AUTO},a.ALIGN_AUTO)),t.setJustifyContent(x(r.justifyContent,{center:a.JUSTIFY_CENTER,"flex-start":a.JUSTIFY_FLEX_START,"flex-end":a.JUSTIFY_FLEX_END,"space-between":a.JUSTIFY_SPACE_BETWEEN,"space-around":a.JUSTIFY_SPACE_AROUND},a.JUSTIFY_FLEX_START)),typeof r.flexBasis!="undefined"?t.setFlexBasis(r.flexBasis):r.display==="block"&&(typeof r.width=="undefined"||r.width==="auto")&&(typeof r.maxWidth=="undefined"||r.maxWidth==="auto")&&t.setFlexBasisPercent(100),t.setFlexDirection(x(r.flexDirection,{row:a.FLEX_DIRECTION_ROW,column:a.FLEX_DIRECTION_COLUMN,"row-reverse":a.FLEX_DIRECTION_ROW_REVERSE,"column-reverse":a.FLEX_DIRECTION_COLUMN_REVERSE},a.FLEX_DIRECTION_ROW)),t.setFlexGrow(r.flexGrow||0),t.setFlexShrink(r.flexShrink||1),t.setFlexWrap(x(r.flexWrap,{wrap:a.WRAP_WRAP,nowrap:a.WRAP_NO_WRAP,"wrap-reverse":a.WRAP_WRAP_REVERSE},a.WRAP_WRAP)),typeof r.maxHeight!="undefined"&&t.setMaxHeight(r.maxHeight),typeof r.maxWidth!="undefined"&&t.setMaxWidth(r.maxWidth),typeof r.minHeight!="undefined"&&t.setMinHeight(r.minHeight),typeof r.minWidth!="undefined"&&t.setMinWidth(r.minWidth),t.setOverflow(x(r.overflow,{visible:a.OVERFLOW_VISIBLE,hidden:a.OVERFLOW_HIDDEN},a.OVERFLOW_VISIBLE)),t.setMargin(a.EDGE_TOP,r.marginTop||0),t.setMargin(a.EDGE_BOTTOM,r.marginBottom||0),t.setMargin(a.EDGE_LEFT,r.marginLeft||0),t.setMargin(a.EDGE_RIGHT,r.marginRight||0),t.setBorder(a.EDGE_TOP,r.borderWidth||0),t.setBorder(a.EDGE_BOTTOM,r.borderWidth||0),t.setBorder(a.EDGE_LEFT,r.borderWidth||0),t.setBorder(a.EDGE_RIGHT,r.borderWidth||0),t.setPadding(a.EDGE_TOP,r.paddingTop||0),t.setPadding(a.EDGE_BOTTOM,r.paddingBottom||0),t.setPadding(a.EDGE_LEFT,r.paddingLeft||0),t.setPadding(a.EDGE_RIGHT,r.paddingRight||0),t.setPositionType((r.position,a.POSITION_TYPE_ABSOLUTE,a.POSITION_TYPE_RELATIVE,a.POSITION_TYPE_RELATIVE)),typeof r.top!="undefined"&&t.setPosition(a.EDGE_TOP,r.top),typeof r.bottom!="undefined"&&t.setPosition(a.EDGE_BOTTOM,r.bottom),typeof r.left!="undefined"&&t.setPosition(a.EDGE_LEFT,r.left),typeof r.right!="undefined"&&t.setPosition(a.EDGE_RIGHT,r.right),typeof r.height!="undefined"?t.setHeight(r.height):t.setHeightAuto(),typeof r.width!="undefined"?t.setWidth(r.width):t.setWidthAuto(),[r,M(r)]}import R from"yoga-layout-prebuilt";import{LineBreaker as dt}from"css-line-break";function I({left:t,top:o,width:e,height:i},s,r){let n;if(r)n=s;else{let f=t+e/2,c=o+i/2;n=T([1,0,0,1,f,c],T(s,[1,0,0,1,-f,-c])),s.__parent&&(n=T(s.__parent,n)),s.splice(0,6,...n)}return`matrix(${n.join(",")})`}function Y({left:t,top:o,width:e,height:i,content:s,isInheritingTransform:r},n){let f="",c=1;return n.transform&&(f=I({left:t,top:o,width:e,height:i},n.transform,r)),n.opacity&&(c=+n.opacity),`<text x="${t}" y="${o+i}" width="${e}" height="${i}" fill="${n.color}" font-weight="${n.fontWeight}" font-style="${n.fontStyle}" font-size="${n.fontSize}" font-family="${n.fontFamily}" ${n.letterSpacing?`letter-spacing="${n.letterSpacing}"`:""} ${f?`transform="${f}"`:""} ${c!==1?`opacity="${c}"`:""}>${s}</text>`}function*X(t,o){let{parentStyle:e,parent:i,font:s,id:r,isInheritingTransform:n}=o,f=dt(t,{lineBreak:"strict",wordBreak:"normal"}),c=[];for(let u;!(u=f.next()).done;)c.push(u.value.slice());let l=[];i.setAlignItems(R.ALIGN_BASELINE),e.textAlign==="left"?i.setJustifyContent(R.JUSTIFY_FLEX_START):e.textAlign==="center"?i.setJustifyContent(R.JUSTIFY_CENTER):e.textAlign==="right"?i.setJustifyContent(R.JUSTIFY_FLEX_END):e.textAlign==="justify"&&i.setJustifyContent(R.JUSTIFY_SPACE_BETWEEN);for(let u of c){let g=R.Node.create();i.insertChild(g,i.getChildCount());let h=s.measure(u,e),E=(e.letterSpacing||0)*u.length;g.setWidth(h.width+E),g.setHeight(h.ascent*1.2),g.setMargin(R.EDGE_BOTTOM,h.descent*1.2),l.push(g)}let[d,p]=yield,m="";for(let u=0;u<l.length;u++){let g=l[u];e.position==="absolute"&&g.calculateLayout();let{left:h,top:E,width:_,height:S}=g.getComputedLayout();h+=d,E+=p,m+=Y({id:r,left:h,top:E,width:_,height:S,content:c[u],isInheritingTransform:n},e)}return m}import gt from"gradient-parser";function ht(t){return t.type==="literal"?t.value:t.type==="hex"?`#${t.value}`:t.type==="rgb"?`rgb(${t.value.join(",")})`:t.type==="rgba"?`rgba(${t.value.join(",")})`:"transparent"}function H({id:t,width:o},{image:e}){if(e.startsWith("linear-gradient(")){let i=gt.parse(e)[0],s,r,n,f;if(i.orientation.type==="directional")[s,r,n,f]={top:[0,1,0,0],bottom:[0,0,0,1],left:[1,0,0,0],right:[0,0,1,0]}[i.orientation.value];else if(i.orientation.type==="angular"){let u=+i.orientation.value/180*Math.PI-Math.PI/2,g=Math.cos(u),h=Math.sin(u);s=0,r=0,n=g,f=h,n<0&&(s-=n,n=0),f<0&&(r-=f,f=0)}let c=o,l=[];for(let u of i.colorStops){let g=ht(u);if(!l.length&&(l.push({offset:0,color:g}),typeof u.length=="undefined"||u.length.value==="0"))continue;let h=typeof u.length=="undefined"?void 0:u.length.type==="%"?u.length.value/100:u.length.value/c;l.push({offset:h,color:g})}l.length||l.push({offset:0,color:"transparent"});let d=l[l.length-1];d.offset!==1&&(typeof d.offset=="undefined"?d.offset=1:l.push({offset:1,color:d.color}));let p=0,m=1;for(let u=0;u<l.length;u++)if(typeof l[u].offset=="undefined"){for(m<u&&(m=u);typeof l[m].offset=="undefined";)m++;l[u].offset=(l[m].offset-l[p].offset)/(m-p)*(u-p)+l[p].offset}else p=u;return[`satori_bi${t}`,`<linearGradient id="satori_bi${t}" x1="${s}" y1="${r}" x2="${n}" y2="${f}">${l.map(u=>`<stop offset="${u.offset*100}%" stop-color="${u.color}"/>`).join("")}</linearGradient>`]}if(e.startsWith("url(")){let i=e.slice(4,-1);return[`satori_bi${t}`,`<pattern id="satori_bi${t}" patternContentUnits="objectBoundingBox" width="1" height="1"><image href="${i}" x="0" y="0" width="1" height="1"/></pattern>`]}}function P(t,o,e){return e<t+o&&(e/2<t&&e/2<o?t=o=e/2:e/2<t?t=e-o:e/2<o&&(o=e-t)),[t,o]}function L({left:t,top:o,width:e,height:i},s){let{borderTopLeftRadius:r,borderTopRightRadius:n,borderBottomLeftRadius:f,borderBottomRightRadius:c}=s;return r=Math.min(r||0,e,i),n=Math.min(n||0,e,i),f=Math.min(f||0,e,i),c=Math.min(c||0,e,i),!r&&!n&&!f&&!c?"":([r,n]=P(r,n,e),[r,f]=P(r,f,i),[n,c]=P(n,c,i),[f,c]=P(f,c,e),`M${t+r},${o} h${e-r-n} a${n},${n} 0 0 1 ${n},${n} v${i-n-c} a${c},${c} 0 0 1 ${-c},${c} h${c+f-e} a${f},${f} 0 0 1 ${-f},${-f} v${f+r-i} a${r},${r} 0 0 1 ${r},${-r}`)}function A({id:t,width:o,height:e},i){if(!i.shadowColor||!i.shadowOffset||typeof i.shadowRadius=="undefined")return"";let s=Math.min(i.shadowOffset.width-i.shadowRadius*2,0),r=Math.max(i.shadowOffset.width+i.shadowRadius*2+o,o),n=Math.min(i.shadowOffset.height-i.shadowRadius*2,0),f=Math.max(i.shadowOffset.height+i.shadowRadius*2+e,e);return`<defs><filter id="satori_s-${t}" x="${s/o*100}%" y="${n/e*100}%" width="${(r-s)/o*100}%" height="${(f-n)/e*100}%"><feDropShadow dx="${i.shadowOffset.width}" dy="${i.shadowOffset.height}" stdDeviation="${i.shadowRadius}" flood-color="${i.shadowColor}" flood-opacity="1"/></filter></defs>`}function U({id:t,left:o,top:e,width:i,height:s,isInheritingTransform:r},n){if(n.display==="none")return"";let f="rect",c="transparent",l=0,d="",p="",m=[],u=1;if(n.backgroundColor&&m.push(n.backgroundColor),n.borderWidth&&(l=n.borderWidth,c=n.borderColor),n.opacity&&(u=+n.opacity),n.transform&&(d=I({left:o,top:e,width:i,height:s},n.transform,r)),n.backgroundImage){let E=n.backgroundImage.map((_,S)=>H({id:t+"_"+S,width:i,height:s},_)).filter(Boolean);for(let _ of E)p+=_[1],m.push(`url(#${_[0]})`)}let g=L({left:o,top:e,width:i,height:s},n);g&&(f="path");let h=A({width:i,height:s,id:t},n);return m.length||m.push("transparent"),`${p?`<defs>${p}</defs>`:""}${h?`${h}<g filter="url(#satori_s-${t})">`:""}${u!==1?`<g opacity="${u}">`:""}${m.map((E,_)=>E==="transparent"&&!(_===m.length-1&&l)?"":`<${f} x="${o}" y="${e}" width="${i}" height="${s}" fill="${E}" ${_===m.length-1&&l?`stroke="${c}" stroke-width="${l}"`:""} ${g?`d="${g}"`:""} ${d?`transform="${d}"`:""}></${f}>`).join("")}${u!==1?"</g>":""}${h?"</g>":""}`}function z({id:t,left:o,top:e,width:i,height:s,src:r},n){if(n.display==="none")return"";let f="",c=1,l=n.objectFit==="contain"?"xMidYMid":n.objectFit==="cover"?"xMidYMid slice":"none",d=L({left:o,top:e,width:i,height:s},n);d&&(f=`<clipPath id="satori_c-${t}"><path x="${o}" y="${e}" width="${i}" height="${s}" d="${d}"></path></clipPath>`),n.opacity&&(c=+n.opacity);let p=A({width:i,height:s,id:t},n);return`${p}${p?`<g filter="url(#satori_s-${t})">`:""}${f}<image href="${r}" x="${o}" y="${e}" width="${i}" height="${s}" preserveAspectRatio="${l}" ${f?`clip-path="url(#satori_c-${t})"`:""} ${c!==1?`opacity="${c}"`:""}></image>${p?"</g>":""}`}function*N(t,o){let{id:e,inheritedStyle:i,parent:s,font:r}=o;if(t===null||typeof t=="undefined")return yield,"";if(!D(t)||typeof t.type=="function"){let $;if(!D(t))$=X(String(t),o);else{if(Q(t.type))throw new Error("Class component is not supported.");$=N(t.type(t.props),o)}$.next();let k=yield;return $.next(k).value}let{type:n,props:f}=t,{style:c,children:l}=f,d=bt.Node.create();s.insertChild(d,s.getChildCount());let[p,m]=w(d,n,i,c,f),u=p.transform===i.transform;u||(p.transform.__parent=i.transform);let g=typeof l=="undefined"?[]:[].concat(l),h=[],E=0;for(let $ of g){let k=N($,{id:e*g.length+ ++E,parentStyle:p,inheritedStyle:m,isInheritingTransform:!0,parent:d,font:r});k.next(),h.push(k)}let[_,S]=yield;p.position==="absolute"&&d.calculateLayout();let{left:F,top:O,width:j,height:V}=d.getComputedLayout();F+=_,O+=S;let W="";n==="img"?W=z({id:e,left:F,top:O,width:j,height:V,src:f.src,isInheritingTransform:u},p):W=U({id:e,left:F,top:O,width:j,height:V,isInheritingTransform:u},p);for(let $ of h)W+=$.next([F,O]).value;return W}import nt from"opentype.js";var B=class{constructor(o){this.fonts=new Map;for(let e of o){let i=e.data,s="buffer"in i?nt.parse(i.buffer.slice(i.byteOffset,i.byteOffset+i.byteLength)):nt.parse(i);this.defaultFont||(this.defaultFont=s),this.fonts.has(e.name)||this.fonts.set(e.name,[]),this.fonts.get(e.name).push([s,e.weight,e.style])}}get({name:o,weight:e,style:i}){if(!this.fonts.has(o))return this.defaultFont;e==="normal"&&(e=400),e==="bold"&&(e=700);let s=[...this.fonts.get(o)];return s.sort(([r,n,f],[c,l,d])=>{if(n!==l)return n?!l||n===e?-1:l===e?1:e===400&&n===500||e===500&&n===400?-1:e===400&&l===500||e===500&&l===400?1:e<400?n<e&&l<e?l-n:n<e?-1:l<e?1:n-l:e<n&&e<l?n-l:e<n?-1:e<l?1:l-n:1;if(f!==d){if(f===i)return-1;if(d===i)return 1}return-1}),s[0][0]}measure(o,{fontFamily:e,fontSize:i,fontWeight:s=400,fontStyle:r="normal"}){let n=this.get({name:e,weight:s,style:r});return{width:n.getAdvanceWidth(o,i),ascent:n.ascender/n.unitsPerEm*i,descent:-(n.descender/n.unitsPerEm)*i}}};function J({width:t,height:o,content:e},i){return`<svg width="${t}" height="${o}" viewBox="0 0 ${t} ${o}" xmlns="http://www.w3.org/2000/svg">${e}</svg>`}function ot(t,o){let e=new B(o.fonts),i=v.Node.create();i.setWidth(o.width),i.setHeight(o.height),i.setFlexDirection(v.FLEX_DIRECTION_ROW),i.setFlexWrap(v.WRAP_WRAP);let s=N(t,{id:1,parentStyle:{},inheritedStyle:{fontSize:16,fontWeight:"normal",fontFamily:"serif",fontStyle:"normal",lineHeight:1.2,color:"black",opacity:1},parent:i,font:e});s.next(),i.calculateLayout(o.width,o.height,v.DIRECTION_LTR);let r=s.next([0,0]).value;return J({width:o.width,height:o.height,content:r})}export{ot as default};
package/package.json CHANGED
@@ -1,11 +1,52 @@
1
1
  {
2
2
  "name": "satori",
3
- "version": "0.0.0",
4
- "description": "",
5
- "main": "index.js",
3
+ "version": "0.0.1",
4
+ "description": "A lightweight library to convert HTML JSX to SVG string.",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.ts",
8
+ "files": [
9
+ "dist/**"
10
+ ],
6
11
  "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1"
12
+ "dev:preview": "vite test/preview",
13
+ "dev:test": "vitest",
14
+ "test": "vitest run",
15
+ "build": "tsup src/index.ts --dts --minify --format cjs,esm"
8
16
  },
9
- "author": "",
10
- "license": "ISC"
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/vercel/satori.git"
20
+ },
21
+ "keywords": [
22
+ "HTML",
23
+ "JSX",
24
+ "SVG",
25
+ "converter",
26
+ "renderer"
27
+ ],
28
+ "author": "Shu Ding <g@shud.in>",
29
+ "license": "MIT",
30
+ "bugs": {
31
+ "url": "https://github.com/vercel/satori/issues"
32
+ },
33
+ "homepage": "https://github.com/vercel/satori#readme",
34
+ "devDependencies": {
35
+ "@types/node": "^17.0.13",
36
+ "@types/opentype.js": "^1.3.3",
37
+ "@types/react": "^17.0.38",
38
+ "react": "^17.0.2",
39
+ "tsup": "^5.11.13",
40
+ "typescript": "^4.5.5",
41
+ "vitest": "^0.2.5"
42
+ },
43
+ "dependencies": {
44
+ "css-background-parser": "^0.1.0",
45
+ "css-line-break": "^2.1.0",
46
+ "css-to-react-native": "^3.0.0",
47
+ "gradient-parser": "^1.0.2",
48
+ "opentype.js": "^1.3.4",
49
+ "parse-css-dimension": "^1.1.0",
50
+ "yoga-layout-prebuilt": "^1.10.0"
51
+ }
11
52
  }