-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Ahmed <ahmed@novalabs-qa.com>
- Loading branch information
1 parent
9af07fe
commit 8350154
Showing
27 changed files
with
602 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
"@react-pdf/textkit": minor | ||
"@react-pdf/layout": minor | ||
"@react-pdf/fns": minor | ||
"@react-pdf/pdfkit": patch | ||
"@react-pdf/image": patch | ||
--- | ||
|
||
feat: bidi support |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const repeat = (list, length = 0) => { | ||
const result = new Array(length); | ||
for (let i = 0; i < length; i += 1) { | ||
result[i] = list; | ||
} | ||
return result; | ||
}; | ||
|
||
export default repeat; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { describe, expect, test } from 'vitest'; | ||
|
||
import repeat from '../src/repeat'; | ||
|
||
describe('repeat', () => { | ||
test('should repeat property times', () => { | ||
expect(repeat('a', 5)).toEqual(['a', 'a', 'a', 'a', 'a']); | ||
expect(repeat('b', 0)).toEqual([]); | ||
expect(repeat('Lorem', 1)).toEqual(['Lorem']); | ||
expect(repeat('Ipsum', 2)).toEqual(['Ipsum', 'Ipsum']); | ||
expect(repeat(undefined, 3)).toEqual([undefined, undefined, undefined]); | ||
}); | ||
|
||
test('should not repeat property', () => { | ||
expect(repeat('Lorem')).toEqual([]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import bidiFactory from 'bidi-js'; | ||
|
||
const bidi = bidiFactory(); | ||
|
||
/** | ||
* @param {Object} layout options | ||
* @param {Object} attributed string | ||
* @return {Object} attributed string | ||
*/ | ||
const bidiEngine = () => (attributedString) => { | ||
const { string } = attributedString; | ||
const direction = attributedString.runs[0]?.attributes.direction; | ||
|
||
const { levels } = bidi.getEmbeddingLevels(string, direction); | ||
|
||
let lastLevel = null; | ||
let lastIndex = 0; | ||
let index = 0; | ||
const res = []; | ||
|
||
for (let i = 0; i < levels.length; i += 1) { | ||
const level = levels[i]; | ||
|
||
if (level !== lastLevel) { | ||
if (lastLevel !== null) { | ||
res.push({ | ||
start: lastIndex, | ||
end: index, | ||
attributes: { bidiLevel: lastLevel }, | ||
}); | ||
} | ||
|
||
lastIndex = index; | ||
lastLevel = level; | ||
} | ||
|
||
index += 1; | ||
} | ||
|
||
if (lastIndex < string.length) { | ||
res.push({ | ||
start: lastIndex, | ||
end: string.length, | ||
attributes: { bidiLevel: lastLevel }, | ||
}); | ||
} | ||
|
||
return { string, runs: res }; | ||
}; | ||
|
||
export default bidiEngine; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import bidiFactory from 'bidi-js'; | ||
import { repeat } from '@react-pdf/fns'; | ||
|
||
const bidi = bidiFactory(); | ||
|
||
const getBidiLevels = (runs) => { | ||
return runs.reduce((acc, run) => { | ||
const length = run.end - run.start; | ||
const levels = repeat(run.attributes.bidiLevel, length); | ||
return acc.concat(levels); | ||
}, []); | ||
}; | ||
|
||
/** | ||
* Perform bidi mirroring | ||
*/ | ||
const mirrorString = () => { | ||
/** | ||
* @param {AttributedString} attributedString attributed string | ||
* @returns {AttributedString} attributed string | ||
*/ | ||
return (attributedString) => { | ||
const levels = getBidiLevels(attributedString.runs); | ||
|
||
let updatedString = ''; | ||
attributedString.string.split('').forEach((char, index) => { | ||
const isRTL = levels[index] % 2 === 1; | ||
const mirroredChar = isRTL | ||
? bidi.getMirroredCharacter(attributedString.string.charAt(index)) | ||
: null; | ||
updatedString += mirroredChar || char; | ||
}); | ||
|
||
return { ...attributedString, string: updatedString, levels }; | ||
}; | ||
}; | ||
|
||
export default mirrorString; |
Oops, something went wrong.