Skip to content
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
5 changes: 3 additions & 2 deletions docs/ja/pdf/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ createReportDrawer
const drawer = createReportDrawer({
report,
getImagePath,
loadErrorImage,
loadErrorImage, // オプション
fonts,
});
```

`report`: レポートの定義を格納したオブジェクト
`getImagePath`: 画像のパスを返す関数
`loadErrorImage`: バーコードのエラーの際に表示する画像を返す関数
`loadErrorImage`: (オプション) バーコードのエラーの際に表示する画像を返す関数
`fonts`: フォントの定義を格納した `PdfFont` オブジェクト

### getImagePath
Expand All @@ -42,6 +42,7 @@ function getImagePath(src: string): string {
### loadErrorImage

バーコードに渡したデータがエラーの場合に表示する画像を返す関数を指定します。
指定しなかった場合は、ライブラリが用意した画像が表示されます。
通常は次のような関数を渡すことになります。

```ts
Expand Down
8 changes: 2 additions & 6 deletions packages/component/src/settings/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// noinspection SpellCheckingInspection

/**
* Index
*
Expand Down Expand Up @@ -101,6 +99,7 @@ export type SettingData = {
barcode: BarcodeOptions;
}

// noinspection SpellCheckingInspection
const noImageUrl = 'data:image/svg+xml;base64,' +
'PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz5cbjxzdmcgaWQ9Il9sYXllcl8' +
'xIiBkYXRhLW5hbWU9ImxheWVyIDEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2Z' +
Expand All @@ -117,6 +116,7 @@ const noImageUrl = 'data:image/svg+xml;base64,' +
'hbWlseTogaGVsdmV0aWNhLCBhcmlhbCwgc2Fucy1zZXJpZjsgZm9udC1zaXplOiAxMnB4OyI-PH' +
'RzcGFuIHg9IjAiIHk9IjAiPk5PIElNQUdFPC90c3Bhbj48L3RleHQ-XG48L3N2Zz5cbg'

// noinspection SpellCheckingInspection
const errorImageUrl = 'data:image/svg+xml;base64, PD94bWwgdmVyc2lvbj0iMS4wIiBl' +
'bmNvZGluZz0iVVRGLTgiPz4KPHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmc' +
'iIHdpZHRoPSIyMDAiIGhlaWdodD0iMTAwIiB2aWV3Qm94PSIwIDAgMjAwIDEwMCI-CjxyZWN0IH' +
Expand Down Expand Up @@ -210,7 +210,3 @@ export const defaultSettings: SettingData = {
errorImageUrl,
}
}

export default function index() {

}
5 changes: 3 additions & 2 deletions packages/pdf/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ createReportDrawer
const drawer = createReportDrawer({
report,
getImagePath,
loadErrorImage,
loadErrorImage, // option
fonts,
});
```

`report`: an object containing the report definition
`getImagePath`: an image

`loadErrorImage`: function to return the image to display in case of barcode errors
`loadErrorImage`: (option) function to return the image to display in case of barcode errors
`fonts`: `PdfFont` object containing the definition of fonts

### getImagePath
Expand All @@ -43,6 +43,7 @@ function getImagePath(src: string): string {
### loadErrorImage

Function to return an image to display if the data passed to the barcode is an error to return an image to be displayed if the data passed to the barcode is an error.
If you do not specify, the image prepared by the library will be displayed.
Usually, you will pass a function like the following.

```ts
Expand Down
4 changes: 0 additions & 4 deletions packages/pdf/src/__test_assets__/drawerTestHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@ export const getImagePath = (src: string) => {
return `${__dirname}/images/no_image.svg`;
}

export function loadErrorImage() {
return fs.readFileSync(`${__dirname}/images/barcode_error.svg`, 'utf8');
}

export const borders = {
solid: { width: 0.25, color: '#000000', style: StyleType.SOLID },
bold: { width: 5, color: '#000000', style: StyleType.SOLID },
Expand Down
10 changes: 5 additions & 5 deletions packages/pdf/src/__test_assets__/images/barcode_error.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions packages/pdf/src/__test_assets__/images/no_image.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions packages/pdf/src/__test_assets__/pdf_makers/barcodeDrawer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Created by sunvisor on 2025/02/14.
* Copyright (C) Sunvisor Lab. 2025.
*/
import { createDocAndScale, drawCaption, getWritePdf, loadErrorImage } from '../drawerTestHelper';
import { createDocAndScale, drawCaption, getWritePdf } from '../drawerTestHelper';
import { BarcodeData } from '@sunvisor/super-leopard-core';
import { BarcodeDrawer } from '../../shapeDrawer/BarcodeDrawer';
import { createBarcode } from '@sunvisor/super-leopard-core';
Expand Down Expand Up @@ -182,7 +182,7 @@ const data: DrawData[] = [
]

export default async function barcodeDrawer() {
const drawer = new BarcodeDrawer({ doc, scale, loadErrorImage });
const drawer = new BarcodeDrawer({ doc, scale });

data.forEach(datum => {
const { caption, config, opacity } = datum;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { createReportDrawer } from '../../reportDrawer/createReportDrawer';
import { testAssets } from '@sunvisor/super-leopard-core';
import { createTestFonts } from '../textTestHelper';
import { getImagePath, getWritePdf, loadErrorImage } from '../drawerTestHelper';
import { getImagePath, getWritePdf } from '../drawerTestHelper';


const { billTestData, billValues, dummyBillRecords } = testAssets;
Expand All @@ -27,7 +27,6 @@ export default async function listReportDrawer() {
const drawer = createReportDrawer({
report,
getImagePath,
loadErrorImage,
fonts: createTestFonts()
});
data.forEach((values, index) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Created by sunvisor on 2025/02/05.
* Copyright (C) Sunvisor Lab. 2025.
*/
import { getImagePath, getWritePdf, loadErrorImage } from '../drawerTestHelper';
import { getImagePath, getWritePdf } from '../drawerTestHelper';
import { ReportData } from '@sunvisor/super-leopard-core';
import { createTestFonts } from '../textTestHelper';
import { createReportDrawer } from '../../reportDrawer/createReportDrawer';
Expand Down Expand Up @@ -39,7 +39,6 @@ export default async function pageReportDrawer() {
const drawer = createReportDrawer({
report,
getImagePath,
loadErrorImage,
fonts: createTestFonts()
});

Expand Down
2 changes: 0 additions & 2 deletions packages/pdf/src/reportDrawer/ListReportDrawer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { createPage, createScale, ReportData } from '@sunvisor/super-leopard-cor
import {
createAndRegisterTestFonts,
getImagePath,
loadErrorImage,
mockDoc,
mockMeasurement,
testAssets
Expand All @@ -29,7 +28,6 @@ function createDrawer(report: ReportData) {
doc: mockDoc,
scale,
getImagePath,
loadErrorImage,
fonts: createAndRegisterTestFonts(mockDoc),
measurement: mockMeasurement,
});
Expand Down
2 changes: 0 additions & 2 deletions packages/pdf/src/reportDrawer/PageReportDrawer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { PageReportDrawer } from "./PageReportDrawer";
import {
createAndRegisterTestFonts,
getImagePath,
loadErrorImage,
mockDoc,
mockMeasurement,
testAssets
Expand All @@ -28,7 +27,6 @@ function createDrawer(report: ReportData) {
doc: mockDoc,
scale,
getImagePath,
loadErrorImage,
fonts: createAndRegisterTestFonts(mockDoc),
measurement: mockMeasurement,
});
Expand Down
2 changes: 0 additions & 2 deletions packages/pdf/src/reportDrawer/PdfDrawer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { describe } from 'vitest';
import {
createAndRegisterTestFonts,
getImagePath,
loadErrorImage,
mockDoc,
mockMeasurement,
testAssets
Expand All @@ -26,7 +25,6 @@ describe('Tests for PdfDrawer', () => {
doc: mockDoc,
scale,
getImagePath,
loadErrorImage,
fonts: createAndRegisterTestFonts(mockDoc),
measurement: mockMeasurement,
});
Expand Down
3 changes: 1 addition & 2 deletions packages/pdf/src/reportDrawer/createPdfDrawer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
import { createPdfDrawer } from "./createPdfDrawer";
import { createPage, UnitType } from '@sunvisor/super-leopard-core';
import { createAndRegisterTestFonts, getImagePath, loadErrorImage, mockDoc } from '../__test_assets__';
import { createAndRegisterTestFonts, getImagePath, mockDoc } from '../__test_assets__';
import { expect } from 'vitest';
import { PdfDrawer } from './PdfDrawer';

Expand All @@ -21,7 +21,6 @@ describe('Tests for createSvgDrawer', () => {
page,
fonts,
getImagePath,
loadErrorImage,
});
// Assert
expect(drawer).toBeDefined();
Expand Down
5 changes: 2 additions & 3 deletions packages/pdf/src/reportDrawer/createReportDrawer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { createReportDrawer } from "./createReportDrawer";
import { ReportData } from '@sunvisor/super-leopard-core';
import { PageReportDrawer } from './PageReportDrawer';
import { createTestFonts, getImagePath, loadErrorImage, testAssets } from '../__test_assets__';
import { createTestFonts, getImagePath, testAssets } from '../__test_assets__';
import { ListReportDrawer } from './ListReportDrawer';


Expand All @@ -32,7 +32,6 @@ describe('Tests for createReportDrawer', () => {
const drawer = createReportDrawer({
report,
getImagePath,
loadErrorImage,
fonts: createTestFonts(),
})
// Assert
Expand All @@ -59,7 +58,7 @@ describe('Tests for createReportDrawer', () => {
};
// Act
const drawer = createReportDrawer({
report, getImagePath, loadErrorImage, fonts: createTestFonts()
report, getImagePath, fonts: createTestFonts()
});
// Assert
expect(drawer).toBeInstanceOf(ListReportDrawer);
Expand Down
9 changes: 4 additions & 5 deletions packages/pdf/src/shapeDrawer/BarcodeDrawer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
* Created by sunvisor on 2025/02/10.
* Copyright (C) Sunvisor Lab. 2025.
*/
import { BarcodeDrawer } from "./BarcodeDrawer";
import { BarcodeDrawer, defaultErrorImageLoader } from "./BarcodeDrawer";
import { BarcodeData, createBarcode, Scale } from '@sunvisor/super-leopard-core';
import { PdfDocumentInterface } from '../pdfDriver/PdfDriverInterface';
import { loadErrorImage } from '../__test_assets__';
import { createBarcodeSvg } from '@sunvisor/super-leopard-barcode';
import { afterEach } from 'vitest';

Expand Down Expand Up @@ -37,7 +36,7 @@ describe('Tests for BarcodeDrawer', () => {
type: 'barcode', format: 'code128', value: '12345', x: 0, y: 10, width: 200, height: 100
}
const barcode = createBarcode(barcodeData);
const drawer = new BarcodeDrawer({ doc, scale, loadErrorImage });
const drawer = new BarcodeDrawer({ doc, scale });
// Act
drawer.draw(barcode, {})
// Assert
Expand All @@ -53,12 +52,12 @@ describe('Tests for BarcodeDrawer', () => {
type: 'barcode', format: 'code128', value: '12345', x: 0, y: 10, width: 200, height: 100
}
const barcode = createBarcode(barcodeData);
const drawer = new BarcodeDrawer({ doc, scale, loadErrorImage });
const drawer = new BarcodeDrawer({ doc, scale });
// Act
drawer.draw(barcode, {})
// Assert
expect(doc.image).toHaveBeenCalledWith({
svg: loadErrorImage(), x:0, y: 10, width: 200, height: 100
svg: defaultErrorImageLoader() , x:0, y: 10, width: 200, height: 100
});
});

Expand Down
33 changes: 32 additions & 1 deletion packages/pdf/src/shapeDrawer/BarcodeDrawer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class BarcodeDrawer implements BarcodeDrawerInterface {
constructor({ doc, scale, loadErrorImage }: BarcodeDrawerProps) {
this.#doc = doc;
this.#scale = scale;
this.#loadErrorImage = loadErrorImage;
this.#loadErrorImage = loadErrorImage ?? defaultErrorImageLoader;
}

draw(barcode: Barcode, params?: DrawerParams): void {
Expand All @@ -45,4 +45,35 @@ export class BarcodeDrawer implements BarcodeDrawerInterface {

}

export function defaultErrorImageLoader() {
return `
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" width="200" height="100" viewBox="0 0 200 100">
<!-- background -->
<rect width="200" height="100" fill="#ffffff" stroke="#000000" stroke-width="2"/>

<!-- broken barcode -->
<line x1="10" y1="20" x2="10" y2="70" stroke="#000" stroke-width="3"/>
<line x1="25" y1="25" x2="25" y2="75" stroke="#000" stroke-width="4"/>
<line x1="40" y1="15" x2="40" y2="65" stroke="#000" stroke-width="2"/>
<line x1="55" y1="30" x2="55" y2="80" stroke="#000" stroke-width="5"/>
<line x1="70" y1="20" x2="70" y2="65" stroke="#000" stroke-width="3"/>
<line x1="85" y1="10" x2="85" y2="60" stroke="#000" stroke-width="4"/>
<line x1="100" y1="25" x2="100" y2="65" stroke="#000" stroke-width="2"/>
<line x1="115" y1="15" x2="115" y2="65" stroke="#000" stroke-width="5"/>
<line x1="130" y1="30" x2="130" y2="65" stroke="#000" stroke-width="3"/>
<line x1="145" y1="20" x2="145" y2="70" stroke="#000" stroke-width="4"/>
<line x1="160" y1="10" x2="160" y2="60" stroke="#000" stroke-width="2"/>
<line x1="175" y1="25" x2="175" y2="75" stroke="#000" stroke-width="5"/>
<line x1="190" y1="15" x2="190" y2="65" stroke="#000" stroke-width="3"/>

<!-- error mark -->
<line x1="10" y1="10" x2="190" y2="90" stroke="#d3d3d3" stroke-width="7" stroke-linecap="round"/>
<line x1="190" y1="10" x2="10" y2="90" stroke="#d3d3d3" stroke-width="7" stroke-linecap="round"/>

<!-- ERROR text -->
<text x="100" y="85" font-family="Helvetica" font-size="20" font-weight="bold" text-anchor="middle" fill="#808080">ERROR</text>
</svg>
`
}

4 changes: 2 additions & 2 deletions packages/pdf/src/shapeDrawer/ShapeDrawer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ export type TextDrawerProps = ShapeDrawerProps & {
}

export type BarcodeDrawerProps = ShapeDrawerProps & {
loadErrorImage: () => string;
loadErrorImage?: () => string;
}

export type ImageHandlerProps = {
getImagePath: GetPdfImagePath;
loadErrorImage: () => string;
loadErrorImage?: () => string;
}

export type PdfDrawerProps = TextDrawerProps & ImageHandlerProps;
Expand Down