Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TextPath & TextBlob support #181

Merged
merged 35 commits into from
Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
d4e8658
Implement drawGlyphs
wcandillon Feb 3, 2022
2c43168
Add test
wcandillon Feb 3, 2022
4a2e7db
Merge branch 'main' into feat/drawGlyphs
william-candillon Feb 3, 2022
f6f9beb
:lipstick:
wcandillon Feb 3, 2022
446d7d6
Document Glyphs feature
wcandillon Feb 3, 2022
7fa98d9
Add Font.__typename__
wcandillon Feb 3, 2022
d64b76e
Fix example
wcandillon Feb 3, 2022
797e1e5
:green_heart:
wcandillon Feb 3, 2022
b0b3189
Implement getGlyphIDs
wcandillon Feb 4, 2022
83f72a0
Fix documentation
wcandillon Feb 4, 2022
744a624
Implement more Font methods
wcandillon Feb 4, 2022
3763b70
Rename Typeface to ITypeface and add JsiInstance extension
wcandillon Feb 4, 2022
b2a14cd
Implement all Font methods from CanvasKit
wcandillon Feb 4, 2022
4aed713
Add support for RSXform (#180)
wcandillon Feb 4, 2022
1a410e3
Merge branch 'feat/drawGlyphs' of https://github.com/Shopify/react-na…
wcandillon Feb 4, 2022
ec51b4b
Update JsiSkRSXform.h
wcandillon Feb 4, 2022
6df80db
Update JsiSkRSXform.h
wcandillon Feb 4, 2022
772d641
Update JsiSkCanvas.h
wcandillon Feb 4, 2022
6dd8207
Add Text Blob support
wcandillon Feb 4, 2022
841440e
Merge branch 'feat/drawGlyphs' of https://github.com/Shopify/react-na…
wcandillon Feb 4, 2022
b863a44
Merge branch 'feat/drawGlyphs' into feat/text-blob-support
wcandillon Feb 4, 2022
467afd4
Add getGlyphWidths()
wcandillon Feb 4, 2022
efcc1c0
:wrench:
wcandillon Feb 6, 2022
e36b4a2
:wrench:
wcandillon Feb 6, 2022
1383957
:wrench:
wcandillon Feb 6, 2022
8f7b13b
Merge branch 'main' into feat/drawGlyphs
wcandillon Feb 6, 2022
1e20c19
:green_heart:
wcandillon Feb 6, 2022
641ef68
:arrow_up:
wcandillon Feb 7, 2022
c98c82c
:wrench:
wcandillon Feb 7, 2022
b036790
:lipstick:
wcandillon Feb 7, 2022
abbde8b
Merge branch 'feat/drawGlyphs' into feat/text-blob-support
wcandillon Feb 7, 2022
6569353
Add symmetry to the TextPath properties
wcandillon Feb 7, 2022
e25287f
:wrench:
wcandillon Feb 7, 2022
4e984cf
Merge branch 'main' into feat/text-blob-support
wcandillon Feb 10, 2022
2a9bacd
:green_heart:
wcandillon Feb 10, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions docs/docs/text/blob.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
id: blob
title: Text Blob
sidebar_label: Text Blob
slug: /text/blob
---

A text blob contains glyphs, their positions, and paint attributes specific to text.

| Name | Type | Description |
|:------------|:-----------|:-------------------------------------------------------------|
| blob | `TextBlob` | Text blob |
| x? | `number` | x coordinate of the origin of the entire run. Default is 0 |
| y? | `number` | y coordinate of the origin of the entire run. Default is 0 |

## Example

```tsx twoslash
import {Canvas, TextBlob, Skia} from "@shopify/react-native-skia";


export const HelloWorld = () => {
const typeface = Skia.FontMgr.RefDefault().matchFamilyStyle("helvetica");
if (!typeface) {
throw new Error("Helvetica not found");
}
const font = Skia.Font(typeface, 30);
const blob = Skia.TextBlob.MakeFromText("Hello World!", font);
return (
<Canvas style={{ flex: 1 }}>
<TextBlob
blob={blob}
color="blue"
/>
</Canvas>
);
};
```
57 changes: 55 additions & 2 deletions docs/docs/text/fonts.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ sidebar_label: Fonts
slug: /text/fonts
---

By default all the fonts available within your app are available in your Canvas. For instance, you can write the following.
## System Fonts

By default all the fonts available within your app are available in your Canvas.
For instance, you can write the following.

```tsx twoslash
import {Canvas, Text} from "@shopify/react-native-skia";
Expand All @@ -16,11 +19,61 @@ export const HelloWorld = () => {
<Text
x={0}
y={0}
value="Hello World"
text="Hello World"
familyName="serif"
size={16}
/>
</Canvas>
);
};
```

System fonts can also be accessed as a font instance using the system font manager.

```tsx twoslash
import {Canvas, Text, Skia} from "@shopify/react-native-skia";

const typeface = Skia.FontMgr.RefDefault().matchFamilyStyle("helvetica");
if (!typeface) {
throw new Error("Helvetica not found");
}
const font = Skia.Font(typeface, 30);

export const HelloWorld = () => {
return (
<Canvas style={{ flex: 1 }}>
<Text
x={0}
y={0}
text="Hello World"
font={font}
/>
</Canvas>
);
};
```

## Custom Fonts

Alternatively, you can use your own set of custom fonts to be available in the canvas, as seen below.

```tsx twoslash
import {Canvas, Text, useFont} from "@shopify/react-native-skia";

export const HelloWorld = () => {
const font = useFont(require("./my-custom-font.otf"), 16);
if (font === null) {
return null;
}
return (
<Canvas style={{ flex: 1 }}>
<Text
x={0}
y={0}
font={font}
text="Hello World"
/>
</Canvas>
);
};
```
41 changes: 41 additions & 0 deletions docs/docs/text/glyphs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
id: glyphs
title: Glyphs
sidebar_label: Glyphs
slug: /text/glyphs
---

This component raws a run of glyphs, at corresponding positions, in a given font.

| Name | Type | Description |
|:------------|:-----------|:-----------------------------------------------------------------------|
| glyphs | `Ghlyph[]` | Glyphs to draw |
| x? | `number`. | x coordinate of the origin of the entire run. Default is 0 |
| y? | `number`. | y coordinate of the origin of the entire run. Default is 0 |
| font | `Font` | Font to use (see [Fonts](/docs/text/fonts)) |
| familyName? | `string` | Font family name to use (see [Fonts](/docs/text/fonts)) |
| size? | `number` | Font size if `familName` is provided |

## Draw text vertically

```tsx twoslash
import {Canvas, Glyphs, vec, useFont} from "@shopify/react-native-skia";

export const HelloWorld = () => {
const font = useFont(require("./my-font.otf"), 16);
if (font === null) {
return null;
}
const glyphs = font
.getGlyphIDs("Hello World!")
.map((id, i) => ({ id, pos: vec(0, i*32) }));
return (
<Canvas style={{ flex: 1 }}>
<Glyphs
font={font}
glyphs={glyphs}
/>
</Canvas>
);
}
```
41 changes: 41 additions & 0 deletions docs/docs/text/path.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
id: path
title: Text Path
sidebar_label: Text Path
slug: /text/path
---

Draws text along an SVG path.
The fonts available in the canvas are described in [here](/docs/text/fonts).

| Name | Type | Description |
|:------------|:-------------------|:-------------------------------------------------------------|
| path | `Path` or `string` | Path to draw. Can be a string using the [SVG Path notation](https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Paths#line_commands) or an object created with `Skia.Path.Make()` |
| text | `string` | Text to draw |
| font | `Font` | Font to use (see [Fonts](/docs/text/fonts)) |
| familyName? | `string` | Font family name to use (see [Fonts](/docs/text/fonts)) |
| size? | `number` | Font size if `familName` is provided |

## Example

```tsx twoslash
import {Canvas, Group, TextPath, Skia} from "@shopify/react-native-skia";

const circle = Skia.Path.Make();
circle.addCircle(128, 128, 128);

export const HelloWorld = () => {
return (
<Canvas style={{ flex: 1 }}>
<Group transform={[{ translateY: 25 }]}>
<TextPath
path={circle}
familyName="helvetica"
size={24}
text="Hello World!"
/>
</Group>
</Canvas>
);
};
```
39 changes: 7 additions & 32 deletions docs/docs/text/text.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ slug: /text/text
---

The text component can be used to draw a simple text.
The font family and the font size must be specified.
The fonts available in the canvas are described in [here](/docs/text/fonts).

| Name | Type | Description |
|:-----------|:----------|:--------------------------------------------------------------|
| value | `string` | Text to draw |
| familyName | `string` | Font family name |
| size | `number` | Font size |
| font | `font` | Custom font to use (loaded with useFont) |
| Name | Type | Description |
|:------------|:----------|:--------------------------------------------------------------|
| text | `string` | Text to draw |
| font | `Font` | Font to use (see [Fonts](/docs/text/fonts)) |
| familyName? | `string` | Font family name to use (see [Fonts](/docs/text/fonts)) |
| size? | `number` | Font size if `familName` is provided |

### Example

Expand All @@ -27,7 +26,7 @@ export const HelloWorld = () => {
<Text
x={0}
y={0}
value="Hello World"
text="Hello World"
familyName="serif"
size={32}
/>
Expand All @@ -36,27 +35,3 @@ export const HelloWorld = () => {
};
```

## Using a custom font

Alternatively, you can use your own set of custom fonts to be available in the canvas, as seen below.

```tsx twoslash
import {Canvas, Text, useFont} from "@shopify/react-native-skia";

export const HelloWorld = () => {
const font = useFont(require("./my-custom-font.otf"), 16);
if (font === null) {
return null;
}
return (
<Canvas style={{ flex: 1 }}>
<Text
x={0}
y={0}
font={font}
value="Hello World"
/>
</Canvas>
);
};
```
8 changes: 7 additions & 1 deletion docs/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@ const sidebars = {
collapsed: true,
type: "category",
label: "Text",
items: ["text/fonts", "text/text"],
items: [
"text/fonts",
"text/text",
"text/glyphs",
"text/path",
"text/blob",
],
},
{
collapsed: true,
Expand Down
16 changes: 16 additions & 0 deletions example/src/Examples/API/Path2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
Path,
Group,
rect,
TextPath,
useFont,
} from "@shopify/react-native-skia";

import { Title } from "./components/Title";
Expand Down Expand Up @@ -43,6 +45,10 @@ const result = Skia.Path.MakeFromOp(rect1, circle, PathOp.Difference)!;
result.simplify();

export const PathExample = () => {
const font = useFont(require("./Roboto-Regular.otf"), 32);
if (!font) {
return null;
}
return (
<ScrollView>
<Title>Path Operations</Title>
Expand All @@ -68,6 +74,12 @@ export const PathExample = () => {
<Canvas style={styles.stroke}>
<Path path={s} color="black" style="stroke" strokeWidth={1} />
</Canvas>
<Title>Text Path</Title>
<Canvas style={styles.textPath}>
<Group transform={[{ translateY: 25 }]}>
<TextPath path={circle} font={font} text="Hello World!" />
</Group>
</Canvas>
</ScrollView>
);
};
Expand All @@ -89,4 +101,8 @@ const styles = StyleSheet.create({
width: SIZE,
height: SIZE,
},
textPath: {
width: SIZE,
height: SIZE,
},
});
Binary file added example/src/Examples/API/Roboto-Regular.otf
Binary file not shown.
2 changes: 1 addition & 1 deletion example/src/Examples/Graphs/Slider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export const Slider: React.FC<GraphProps> = ({ height, width }) => {
size={12}
x={() => touchPos.value.x - 24}
y={() => touchPos.value.y - 18}
value={() => "$ " + (touchPos.value.y * -1).toFixed(2)}
text={() => "$ " + (touchPos.value.y * -1).toFixed(2)}
/>
<Line
p1={() => vec(touchPos.value.x, touchPos.value.y + 14)}
Expand Down
4 changes: 3 additions & 1 deletion example/src/Examples/Matrix/Matrix.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,17 @@ export const Matrix = () => {
if (font === null) {
return null;
}
const symbols = font.getGlyphIDs("abcdefghijklmnopqrstuvwxyz");
return (
<Canvas style={{ flex: 1 }}>
<Fill color="black" />
<Paint>
<BlurMask sigma={10} style="solid" />
<BlurMask sigma={8} style="solid" />
</Paint>
{cols.map((_i, i) =>
rows.map((_j, j) => (
<Symbol
symbols={symbols}
font={font}
timestamp={timestamp}
key={`${i}-${j}`}
Expand Down
Loading