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

🐛 Fix text rise not always reset #37

Merged
merged 1 commit into from
Apr 15, 2023
Merged
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
11 changes: 9 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# Changelog

## [0.4.0] - Unreleased
## [0.4.1] - Unreleased

### Fixed

* Text rise is now reset properly and does not affect subsequent text
elements anymore.

## [0.4.0] - 2023-03-27

### Breaking changes

Expand All @@ -13,7 +20,7 @@
* Block attribute `verticalAlign` for vertical alignment of columns.
* Attribute `lineDash` for graphics shapes.

## [0.3.3] - 2022-03-03
## [0.3.3] - 2023-03-03

### Fixed

Expand Down
10 changes: 1 addition & 9 deletions examples/sample.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,7 @@ export default {
{ text: 'cillum dolore', color: 'red' },
' eu fugiat nulla pariatur: ',
{
text: [
'x',
{ text: 'ⁿ⁻¹', rise: 3 },
' 10',
{ text: '³', rise: 3 },
' H',
{ text: '₂', rise: -3 },
'O',
],
text: ['H', { text: '₂', rise: -3 }, 'O 10', { text: '⁻³', rise: 3 }],
color: '#0066cc',
},
],
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pdfmkr",
"version": "0.4.0",
"version": "0.4.1",
"description": "Generate PDF documents and from JavaScript objects",
"license": "MIT",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion src/layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export function layoutPages(def: DocumentDefinition, doc: Document): Page[] {
pages.push({ size: doc.pageSize, content: frame, header, footer });
}

//Re-layout headers and footers to provide them with the final page count.
// Re-layout headers and footers to provide them with the final page count.
pages.forEach((page, idx) => {
const pageInfo = { pageCount: pages.length, pageNumber: idx + 1, pageSize: doc.pageSize };
typeof def.header === 'function' && (page.header = layoutHeader(def.header(pageInfo), doc));
Expand Down
5 changes: 4 additions & 1 deletion src/page.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { PDFFont, PDFImage, PDFName, PDFPage } from 'pdf-lib';
import { Color, PDFFont, PDFImage, PDFName, PDFPage } from 'pdf-lib';

import { Size } from './box.js';
import { Frame } from './layout.js';

export type TextState = { color?: Color; font?: string; size?: number; rise?: number };

export type Page = {
size: Size;
content: Frame;
Expand All @@ -11,6 +13,7 @@ export type Page = {
pdfPage?: PDFPage;
fonts?: { [ref: string]: PDFName };
images?: { [ref: string]: PDFName };
textState?: TextState;
extGStates?: { [ref: string]: PDFName };
};

Expand Down
6 changes: 2 additions & 4 deletions src/render-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import {

import { Pos } from './box.js';
import { TextObject } from './layout.js';
import { getPageFont, Page } from './page.js';
import { getPageFont, Page, TextState } from './page.js';

export function renderText(object: TextObject, page: Page, base: Pos) {
const contentStream: PDFContentStream = (page.pdfPage as any).getContentStream();
const state: TextState = {};
const state = (page.textState ??= {});
const x = base.x;
const y = page.size.height - base.y;
contentStream.push(beginText());
Expand All @@ -40,8 +40,6 @@ export function renderText(object: TextObject, page: Page, base: Pos) {
contentStream.push(endText());
}

type TextState = { color?: Color; font?: string; size?: number; rise?: number };

function setTextColorOp(state: TextState, color?: Color): PDFOperator | undefined {
const effectiveColor = color ?? rgb(0, 0, 0);
if (!equalsColor(state.color, effectiveColor)) {
Expand Down
39 changes: 39 additions & 0 deletions test/render-text.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,45 @@ describe('render-text', () => {
]);
});

it('maintains text state throughout page', () => {
const obj1: TextObject = {
type: 'text',
rows: [
{
segments: [{ text: 'foo', font, fontSize: 10, rise: 3 }],
...{ x: 1, y: 2, width: 60, height: 12, baseline: 8 },
},
],
};
const obj2: TextObject = {
type: 'text',
rows: [
{
segments: [{ text: 'bar', font, fontSize: 10 }],
...{ x: 3, y: 4, width: 60, height: 12, baseline: 8 },
},
],
};

renderText(obj1, page, pos);
renderText(obj2, page, pos);

expect(getContentStream(page)).toEqual([
'BT',
'1 0 0 1 11 770 Tm',
'0 0 0 rg',
'/fontA-1 10 Tf',
'3 Ts',
'foo Tj',
'ET',
'BT',
'1 0 0 1 13 768 Tm',
'0 Ts', // reset text rise
'bar Tj',
'ET',
]);
});

it('renders multiple rows with multiple text segments', () => {
const seg1 = { text: 'foo', font, fontSize: 10 };
const seg2 = { text: 'bar', font, fontSize: 10 };
Expand Down