tycho-components 0.40.5 → 0.40.6

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.
@@ -15,5 +15,6 @@ declare const ConlluUtils: {
15
15
  isNotEmptyConllu: (conllu: Conllu) => boolean;
16
16
  deleteToken: (conllu: Conllu, tokenIndex: number) => Conllu;
17
17
  getConlluTranslationFromStruct: (struct: Struct | undefined, lang: string) => string;
18
+ generateImageFromSVG: (svgElement: SVGSVGElement) => void;
18
19
  };
19
20
  export default ConlluUtils;
@@ -1,3 +1,4 @@
1
+ import { saveAs } from 'file-saver';
1
2
  const convertStructToConllu = (struct) => {
2
3
  return {
3
4
  uid: struct.uid,
@@ -224,6 +225,37 @@ const extractConlluTokens = (conllText) => {
224
225
  const getConlluTranslationFromStruct = (struct, lang) => {
225
226
  return struct?.attributes?.[lang] || '';
226
227
  };
228
+ const generateImageFromSVG = (svgElement) => {
229
+ const canvas = document.createElement('canvas');
230
+ const context = canvas.getContext('2d');
231
+ if (!context) {
232
+ throw new Error('Unable to get canvas context');
233
+ }
234
+ const { width, height } = svgElement.getBBox();
235
+ canvas.width = width;
236
+ canvas.height = height;
237
+ // Convert SVG element to data URL
238
+ const svgData = new XMLSerializer().serializeToString(svgElement);
239
+ const svgBlob = new Blob([svgData], {
240
+ type: 'image/svg+xml;charset=utf-8',
241
+ });
242
+ const url = URL.createObjectURL(svgBlob);
243
+ const img = new Image();
244
+ img.crossOrigin = 'anonymous'; // handle cross-origin issues
245
+ img.onload = async () => {
246
+ context.fillStyle = 'white';
247
+ context.fillRect(0, 0, width, height);
248
+ context.drawImage(img, 0, 0, width, height);
249
+ URL.revokeObjectURL(url);
250
+ // Convert canvas to JPEG blob
251
+ canvas.toBlob((blob) => {
252
+ if (blob === null)
253
+ return;
254
+ saveAs(blob, 'conllu.jpg');
255
+ }, 'image/jpeg', 1.0);
256
+ };
257
+ img.src = url;
258
+ };
227
259
  const ConlluUtils = {
228
260
  convertToSentence,
229
261
  convertToConllu,
@@ -234,5 +266,6 @@ const ConlluUtils = {
234
266
  isNotEmptyConllu,
235
267
  deleteToken,
236
268
  getConlluTranslationFromStruct,
269
+ generateImageFromSVG,
237
270
  };
238
271
  export default ConlluUtils;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "tycho-components",
3
3
  "private": false,
4
- "version": "0.40.5",
4
+ "version": "0.40.6",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "exports": {