Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 7 additions & 0 deletions .changeset/six-paws-tie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@react-pdf/textkit": minor
"@react-pdf/layout": minor
"@react-pdf/types": minor
---

Add support for fontFeatureSettings to customize ligatures, tabular number display, and other font features.
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/* eslint react/prop-types: 0 */
/* eslint react/jsx-sort-props: 0 */

import { Document, Font, Page, StyleSheet, Text } from '@react-pdf/renderer';
import React from 'react';

import RobotoFont from '../../../public/Roboto-Regular.ttf';
import RubikFont from '../../../public/Rubik-Regular.ttf';

const styles = StyleSheet.create({
body: {
paddingTop: 35,
paddingBottom: 45,
paddingHorizontal: 35,
position: 'relative',
fontSize: 14,
},
headline: {
fontFamily: 'Roboto',
fontSize: '24',
paddingVertical: 12,
},
rubik: {
fontFamily: 'Rubik',
},
roboto: {
fontFamily: 'Roboto',
},
tabular: {
fontFeatureSettings: ['tnum'],
},
smallCapitals: {
fontFeatureSettings: ['smcp'],
},
disableCommonLigatures: {
fontFeatureSettings: { liga: 0 },
},
});

Font.register({
family: 'Rubik',
fonts: [{ src: RubikFont, fontWeight: 400 }],
});
Font.register({
family: 'Roboto',
fonts: [{ src: RobotoFont, fontWeight: 400 }],
});

const MyDoc = () => {
const longNumberExample = "012'345'678'901";
const commonLigaturesExample = 'A firefighter from Sheffield';
return (
<Page style={styles.body}>
<Text style={styles.headline}>Rubik</Text>
<Text style={styles.rubik}>{longNumberExample} – Default features</Text>
<Text style={[styles.rubik, styles.tabular]}>
{longNumberExample} – Tabular numbers
</Text>
<Text style={styles.headline}>Roboto</Text>
<Text style={styles.roboto}>
{commonLigaturesExample} – Default features
</Text>
<Text style={[styles.roboto, styles.disableCommonLigatures]}>
{commonLigaturesExample} – Common ligatures off
</Text>
<Text style={[styles.roboto, styles.smallCapitals]}>
{commonLigaturesExample} – Small capitals
</Text>
</Page>
);
};

const FontFeatureSettings = () => {
return (
<Document>
<MyDoc />
</Document>
);
};

export default {
id: 'font-feature-settings',
name: 'Font Feature Settings',
description: '',
Document: FontFeatureSettings,
};
4 changes: 3 additions & 1 deletion packages/examples/vite/src/examples/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import duplicatedImages from './duplicated-images';
import ellipsis from './ellipsis';
import emoji from './emoji';
import fontFamilyFallback from './font-family-fallback';
import fontFeatureSettings from './font-feature-settings';
import fontWeight from './font-weight';
import forms from './forms';
import fractals from './fractals';
import goTo from './go-to';
import imageStressTest from './image-stress-test';
Expand All @@ -18,14 +20,14 @@ import resume from './resume';
import svg from './svg';
import svgTransform from './svg-transform';
import transformOrigin from './transform-origin';
import forms from './forms';

const EXAMPLES = [
duplicatedImages,
ellipsis,
emoji,
fontFamilyFallback,
fontWeight,
fontFeatureSettings,
fractals,
goTo,
JpgOrientation,
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/vite/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import './index.css';

import { PDFViewer } from '@react-pdf/renderer';
import React, { useEffect, useState } from 'react';
import { createRoot } from 'react-dom/client';
import { PDFViewer } from '@react-pdf/renderer';

import EXAMPLES from './examples';

Expand Down
1 change: 1 addition & 0 deletions packages/layout/src/steps/resolveInheritance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const BASE_INHERITABLE_PROPERTIES = [
'fontSize',
'fontStyle',
'fontWeight',
'fontFeatureSettings',
'letterSpacing',
'opacity',
'textDecoration',
Expand Down
1 change: 1 addition & 0 deletions packages/layout/src/svg/inheritProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const BASE_SVG_INHERITED_PROPS = [
'fontSize',
'fontStyle',
'fontWeight',
'fontFeatureSettings',
'letterSpacing',
'opacity',
'textDecoration',
Expand Down
2 changes: 2 additions & 0 deletions packages/layout/src/text/getAttributedString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const getFragments = (
fontWeight,
fontStyle,
fontSize = 18,
fontFeatureSettings,
textAlign,
lineHeight,
textDecoration,
Expand Down Expand Up @@ -100,6 +101,7 @@ const getFragments = (
// @ts-expect-error allow this props access
link: parentLink || instance.props?.src || instance.props?.href,
align: textAlign || (direction === 'rtl' ? 'right' : 'left'),
features: fontFeatureSettings,
};

for (let i = 0; i < instance.children.length; i += 1) {
Expand Down
4 changes: 4 additions & 0 deletions packages/layout/tests/steps/resolveInhritance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,8 @@ describe('layout resolveInheritance', () => {
test('Should inherit textAlign value', shouldInherit('textAlign'));
test('Should inherit visibility value', shouldInherit('visibility'));
test('Should inherit wordSpacing value', shouldInherit('wordSpacing'));
test(
'Should inherit fontFeatureSettings value',
shouldInherit('fontFeatureSettings'),
);
});
32 changes: 32 additions & 0 deletions packages/stylesheet/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,12 +321,44 @@ export type TextTransform =

export type VerticalAlign = 'sub' | 'super';

export type FontFeatureSetting =
| 'liga'
| 'dlig'
| 'onum'
| 'lnum'
| 'tnum'
| 'zero'
| 'frac'
| 'sups'
| 'subs'
| 'smcp'
| 'c2sc'
| 'case'
| 'hlig'
| 'calt'
| 'swsh'
| 'hist'
| 'ss**'
| 'kern'
| 'locl'
| 'rlig'
| 'medi'
| 'init'
| 'isol'
| 'fina'
| 'mark'
| 'mkmk';
export type FontFeatureSettings =
| FontFeatureSetting[]
| Record<FontFeatureSetting, number>;

export type TextStyle = {
direction?: 'ltr' | 'rtl';
fontSize?: number | string;
fontFamily?: string | string[];
fontStyle?: FontStyle;
fontWeight?: FontWeight;
fontFeatureSettings?: FontFeatureSettings;
letterSpacing?: number | string;
lineHeight?: number | string;
maxLines?: number;
Expand Down
4 changes: 2 additions & 2 deletions packages/textkit/src/layout/generateGlyphs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const layoutRun = (string: string) => {
*/
return (run: Run) => {
const { start, end, attributes = {} } = run;
const { font } = attributes;
const { font, features } = attributes;

if (!font) return { ...run, glyphs: [], glyphIndices: [], positions: [] };

Expand All @@ -53,7 +53,7 @@ const layoutRun = (string: string) => {
// passing LTR To force fontkit to not reverse the string
const glyphRun = font[0].layout(
runString,
undefined,
features,
undefined,
undefined,
'ltr',
Expand Down
5 changes: 3 additions & 2 deletions packages/textkit/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Glyph as FontkitGlyph } from 'fontkit';
import type { Font } from '@react-pdf/font';
import type { FontFeatureSettings } from '@react-pdf/stylesheet';
import type { Glyph as FontkitGlyph } from 'fontkit';
import { Factor as JustificationFactor } from './engines/justification/types';

export type Coordinate = {
Expand Down Expand Up @@ -50,7 +51,7 @@ export type Attributes = {
bullet?: unknown;
characterSpacing?: number;
color?: string;
direction?: 'rtl' | 'ltr';
direction?: FontFeatureSettings;
features?: unknown[];
fill?: boolean;
font?: Font[];
Expand Down