Skip to content

Feature/ccmp #688

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

Merged
merged 22 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 19 additions & 0 deletions src/bidi.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import arabicWordCheck from './features/arab/contextCheck/arabicWord.js';
import arabicSentenceCheck from './features/arab/contextCheck/arabicSentence.js';
import arabicPresentationForms from './features/arab/arabicPresentationForms.js';
import arabicRequiredLigatures from './features/arab/arabicRequiredLigatures.js';
import ccmpReplacementCheck from './features/ccmp/contextCheck/ccmpReplacement.js';
import ccmpReplacement from './features/ccmp/ccmpReplacementLigatures.js';
import latinWordCheck from './features/latn/contextCheck/latinWord.js';
import latinLigature from './features/latn/latinLigatures.js';
import thaiWordCheck from './features/thai/contextCheck/thaiWord.js';
Expand Down Expand Up @@ -42,6 +44,7 @@ Bidi.prototype.setText = function (text) {
* arabic sentence check for adjusting arabic layout
*/
Bidi.prototype.contextChecks = ({
ccmpReplacementCheck,
latinWordCheck,
arabicWordCheck,
arabicSentenceCheck,
Expand All @@ -64,6 +67,7 @@ function registerContextChecker(checkId) {
* tokenize text input
*/
function tokenizeText() {
registerContextChecker.call(this, 'ccmpReplacement');
registerContextChecker.call(this, 'latinWord');
registerContextChecker.call(this, 'arabicWord');
registerContextChecker.call(this, 'arabicSentence');
Expand Down Expand Up @@ -160,6 +164,18 @@ function applyArabicPresentationForms() {
}
}

/**
* Apply ccmp replacement
*/
function applyCcmpReplacement() {
checkGlyphIndexStatus.call(this);
const ranges = this.tokenizer.getContextRanges('ccmpReplacement');
for(let i = 0; i < ranges.length; i++) {
const range = ranges[i];
ccmpReplacement.call(this, range);
}
}

/**
* Apply required arabic ligatures
*/
Expand Down Expand Up @@ -223,6 +239,9 @@ Bidi.prototype.checkContextReady = function (contextId) {
* https://learn.microsoft.com/en-us/typography/opentype/spec/features_ae#tag-ccmp
*/
Bidi.prototype.applyFeaturesToContexts = function () {
if (this.checkContextReady('ccmpReplacement')) {
applyCcmpReplacement.call(this);
}
if (this.checkContextReady('arabicWord')) {
applyArabicPresentationForms.call(this);
applyArabicRequireLigatures.call(this);
Expand Down
20 changes: 19 additions & 1 deletion src/features/applySubstitution.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/*
* @Author: TonyJR show_3@163.com
* @Date: 2024-03-18 18:01:38
* @LastEditors: TonyJR show_3@163.com
* @LastEditTime: 2024-03-22 14:40:59
* @FilePath: /opentype.js/src/features/applySubstitution.js
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
import { SubstitutionAction } from './featureQuery.js';

/**
Expand Down Expand Up @@ -50,14 +58,24 @@ function ligatureSubstitutionFormat1(action, tokens, index) {
}
}

function ligatureSubstitutionFormat3(action, tokens, index) {
tokens[index].setState(action.tag, action.substitution);
const compsCount = action.substitution.length;
for (let i = 0; i < compsCount; i++) {
let token = tokens[index + i + 1];
token.setState("deleted", true);
}
}

/**
* Supported substitutions
*/
const SUBSTITUTIONS = {
11: singleSubstitutionFormat1,
12: singleSubstitutionFormat2,
63: chainingSubstitutionFormat3,
41: ligatureSubstitutionFormat1
41: ligatureSubstitutionFormat1,
53: ligatureSubstitutionFormat3
};

/**
Expand Down
56 changes: 56 additions & 0 deletions src/features/ccmp/ccmpReplacementLigatures.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* @Author: TonyJR show_3@163.com
* @Date: 2024-03-19 16:19:59
* @LastEditors: TonyJR show_3@163.com
* @LastEditTime: 2024-03-21 19:31:38
* @FilePath: /opentype.js/src/features/ccmp/ccmp.js
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/

import { ContextParams } from '../../tokenizer.js';
import applySubstitution from '../applySubstitution.js';

// @TODO: use commonFeatureUtils.js for reduction of code duplication
// once #564 has been merged.

/**
* Update context params
* @param {any} tokens a list of tokens
* @param {number} index current item index
*/
function getContextParams(tokens, index) {
const context = tokens.map(token => token.activeState.value);
return new ContextParams(context, index || 0);
}

/**
* Apply Arabic required ligatures to a context range
* @param {ContextRange} range a range of tokens
*/
function ccmpReplacementLigatures(range) {
const script = 'delf';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this should be 'DFLT'?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only place in the codebase with 'delf'.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry this made it through the review process, I didn't follow up because it was marked as resolved. Good catch, thanks!

@TonyJR could you comment on this please? I think it should be DFLT for the default script. And for the future, please let reviewers resolve the conversations. :)

const tag = 'ccmp';
let tokens = this.tokenizer.getRangeTokens(range);
let contextParams = getContextParams(tokens);
for(let index = 0; index < contextParams.context.length; index++) {
if (!this.query.getFeature({tag, script, contextParams})){
continue;
}
contextParams.setCurrentIndex(index);
let substitutions = this.query.lookupFeature({
tag, script, contextParams
});
if (substitutions.length) {
for(let i = 0; i < substitutions.length; i++) {
const action = substitutions[i];
applySubstitution(action, tokens, index);
}
contextParams = getContextParams(tokens);
}
}
}

export default ccmpReplacementLigatures;



22 changes: 22 additions & 0 deletions src/features/ccmp/contextCheck/ccmpReplacement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* @Author: TonyJR show_3@163.com
* @Date: 2024-03-19 16:01:40
* @LastEditors: TonyJR show_3@163.com
* @LastEditTime: 2024-03-19 19:30:41
* @FilePath: /opentype.js/src/features/ccmp/contextCheck/ccmpReplacement.js
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/


function ccmpReplacementStartCheck(contextParams) {
return contextParams.index === 0 && contextParams.context.length > 1;
}

function ccmpReplacementEndCheck(contextParams) {
return contextParams.index === contextParams.context.length - 1;
}

export default {
startCheck: ccmpReplacementStartCheck,
endCheck: ccmpReplacementEndCheck
};
63 changes: 63 additions & 0 deletions src/features/featureQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,13 @@ function chainingSubstitutionFormat3(contextParams, subtable) {
const substitution = lookup(glyphIndex);
if (substitution) substitutions.push(substitution);
}
} else if (substitutionType === "21") {
for (let n = 0; n < inputLookups.length; n++) {
const glyphIndex = contextParams.get(n);
const substitution = lookup(glyphIndex);
if (substitution)
substitutions.concat(substitution);
}
} else {
throw new Error(`Substitution type ${substitutionType} is not supported in chaining substitution`);
}
Expand Down Expand Up @@ -203,6 +210,47 @@ function ligatureSubstitutionFormat1(contextParams, subtable) {
return null;
}

/**
* Handle ligature substitution - format 3
* @param {ContextParams} contextParams context params to lookup
*/
function ligatureSubstitutionFormat3(contextParams, subtable) {
let substitutions = [];

for (let i = 0; i < subtable.coverages.length; i++){
const lookupRecord = subtable.lookupRecords[i];
const coverage = subtable.coverages[i];

let glyphIndex = contextParams.context[contextParams.index + lookupRecord.sequenceIndex];
let ligSetIndex = lookupCoverage(glyphIndex, coverage);
if (ligSetIndex === -1){
return null;
}
let lookUp = this.font.tables.gsub.lookups[lookupRecord.lookupListIndex];
for (let i = 0; i < lookUp.subtables.length; i++){
let subtable = lookUp.subtables[i];
let ligSetIndex = lookupCoverage(glyphIndex, subtable.coverage);
if (ligSetIndex === -1)
return null;
switch (lookUp.lookupType) {
case 1:{
let ligature = subtable.substitute[ligSetIndex];
substitutions.push(ligature);
break;
}
case 2:{
let ligatureSet = subtable.sequences[ligSetIndex];
substitutions.concat(ligatureSet);
break;
}
default:
break;
}
}
}
return substitutions;
}

/**
* Handle decomposition substitution - format 1
* @param {number} glyphIndex glyph index
Expand Down Expand Up @@ -327,8 +375,13 @@ FeatureQuery.prototype.getLookupMethod = function(lookupTable, subtable) {
return glyphIndex => decompositionSubstitutionFormat1.apply(
this, [glyphIndex, subtable]
);
case '53':
return contextParams => ligatureSubstitutionFormat3.apply(
this, [contextParams, subtable]
);
default:
throw new Error(
`substitutionType : ${substitutionType} ` +
`lookupType: ${lookupTable.lookupType} - ` +
`substFormat: ${subtable.substFormat} ` +
'is not yet supported'
Expand Down Expand Up @@ -435,6 +488,16 @@ FeatureQuery.prototype.lookupFeature = function (query) {
}));
}
break;
case "53":
substitution = lookup(contextParams);
if (Array.isArray(substitution) && substitution.length) {
substitutions.splice(currentIndex, 1, new SubstitutionAction({
id: 53,
tag: query.tag,
substitution
}));
}
break;
}
contextParams = new ContextParams(substitutions, currentIndex);
if (Array.isArray(substitution) && !substitution.length) continue;
Expand Down
47 changes: 47 additions & 0 deletions test/emoji.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* @Author: TonyJR show_3@163.com
* @Date: 2024-03-18 18:24:53
* @LastEditors: TonyJR show_3@163.com
* @LastEditTime: 2024-03-22 15:01:25
* @FilePath: /opentype.js/test/emoji.js
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
import assert from 'assert';
import { parse } from '../src/opentype.js';
import { readFileSync } from 'fs';
const loadSync = (url, opt) => parse(readFileSync(url), opt);

describe('noto emoji with ccmp', () => {
let notoEmojiFont;
let substitution;
before(()=> {
notoEmojiFont = loadSync('./test/fonts/noto-emoji.ttf');
});

describe('ccmp features', () => {

it('shape emoji with sub_0', () => {
let options = {
kerning: true,
language: 'dflt',
features: [
{ script: 'DFLT', tags: ['ccmp'] },
]
};
let glyphIndexes = notoEmojiFont.stringToGlyphIndexes('👨‍👩‍👧‍👦👨‍👩‍👧',options);
assert.deepEqual(glyphIndexes, [1463,1462]);
});

it('shape emoji with sub_5', () => {
let options = {
kerning: true,
language: 'dflt',
features: [
{ script: 'DFLT', tags: ['ccmp'] },
]
};
let glyphIndexes = notoEmojiFont.stringToGlyphIndexes('🇺🇺',options);
assert.deepEqual(glyphIndexes, [1850]);
});
});
});
Binary file added test/fonts/noto-emoji.ttf
Binary file not shown.