Skip to content

Commit

Permalink
Merge pull request #1307 from prepare/master
Browse files Browse the repository at this point in the history
update to latest (2020-05-29) version, bug fixes
  • Loading branch information
larsbrubaker authored May 29, 2020
2 parents ef37e5a + cd1f563 commit d712fdb
Show file tree
Hide file tree
Showing 58 changed files with 5,840 additions and 1,432 deletions.
94 changes: 58 additions & 36 deletions Typography.GlyphLayout/GlyphLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ class GlyphLayoutPlanCollection
public GlyphLayoutPlanContext GetPlanOrCreate(Typeface typeface, ScriptLang scriptLang)
{
GlyphLayoutPlanKey key = new GlyphLayoutPlanKey(typeface, scriptLang.internalName);
GlyphLayoutPlanContext context;
if (!_collection.TryGetValue(key, out context))

if (!_collection.TryGetValue(key, out GlyphLayoutPlanContext context))
{
var glyphSubstitution = (typeface.GSUBTable != null) ? new GlyphSubstitution(typeface, scriptLang.shortname) : null;
var glyphPosition = (typeface.GPOSTable != null) ? new GlyphSetPosition(typeface, scriptLang.shortname) : null;
Expand All @@ -166,8 +166,8 @@ public GlyphLayoutPlanContext GetPlanOrCreate(Typeface typeface, ScriptLang scri
}
struct GlyphLayoutPlanKey
{
public Typeface t;
public int scriptInternameName;
public readonly Typeface t;
public readonly int scriptInternameName;
public GlyphLayoutPlanKey(Typeface t, int scriptInternameName)
{
this.t = t;
Expand Down Expand Up @@ -195,7 +195,7 @@ struct dbugCodePointFromUserChar
/// <summary>
/// offset from the start of input codepoint buffer
/// </summary>
public ushort user_char_offset;
public readonly ushort user_char_offset;
public dbugCodePointFromUserChar(ushort user_char_offset, int codePoint)
{
this.user_char_offset = user_char_offset;
Expand Down Expand Up @@ -252,6 +252,7 @@ public ScriptLang ScriptLang

public bool EnableLigature { get; set; }
public bool EnableComposition { get; set; }

public Typeface Typeface
{
get => _typeface;
Expand All @@ -266,8 +267,14 @@ public Typeface Typeface
}


//not thread-safe***


public delegate ushort GlyphNotFoundHandler(GlyphLayout glyphLayout, int codepoint, int nextcodepoint);
GlyphNotFoundHandler _glyphNotFoundHandler;



//not thread-safe***
List<int> _reusableUserCodePoints = new List<int>();
#if DEBUG
List<dbugCodePointFromUserChar> _dbugReusableCodePointFromUserCharList = new List<dbugCodePointFromUserChar>();
Expand Down Expand Up @@ -325,44 +332,60 @@ public void Layout(
Layout(_reusableUserCodePoints);
}

public void SetGlyphIndexNotFoundHandler(GlyphNotFoundHandler glyphNotFoundHandler)
{
_glyphNotFoundHandler = glyphNotFoundHandler;
}
public void Layout(IList<int> inputCodePoints)
{
Layout(inputCodePoints, 0, inputCodePoints.Count);
}
public void Layout(IList<int> inputCodePoints, int startAt, int len)
{

//
//[B]
// convert codepoint-list to input glyph-list
// clear before use
_inputGlyphs.Clear();

int end = startAt + len;
int cur_codepoint, next_codepoint;

for (int i = 0; i < end; ++i)
{
//find glyph index by specific codepoint
ushort glyphIndex = _typeface.LookupIndex(inputCodePoints[i]);
//find glyph index by specific codepoint
if (i + 1 < end)
{
// Maybe this is a UVS sequence; in that case,
//***SKIP*** the second codepoint
ushort variationGlyphIndex = _typeface.LookupIndex(inputCodePoints[i], inputCodePoints[i + 1]);
if (variationGlyphIndex > 0)
{
//user glyph index from next codepoint
glyphIndex = variationGlyphIndex;
//but record as current code point i
_inputGlyphs.AddGlyph(i, glyphIndex);
cur_codepoint = inputCodePoints[i];
next_codepoint = inputCodePoints[i + 1];
}
else
{
cur_codepoint = inputCodePoints[i];
next_codepoint = 0;
}

++i; //skip
continue;//***
}
ushort glyphIndex = _typeface.GetGlyphIndex(cur_codepoint, next_codepoint, out bool skipNextCodepoint);

if (glyphIndex == 0 && _glyphNotFoundHandler != null)
{
//handle glyph not found
glyphIndex = _glyphNotFoundHandler(this, cur_codepoint, next_codepoint);
}

_inputGlyphs.AddGlyph(i, glyphIndex);
if (skipNextCodepoint)
{
// Maybe this is a UVS sequence; in that case,
//***SKIP*** the second codepoint
++i;
}
}
//continue below...
Layout(_inputGlyphs);
}


void Layout(GlyphIndexList glyphs)
{
if (_needPlanUpdate)
Expand All @@ -377,7 +400,7 @@ void Layout(GlyphIndexList glyphs)
{
//TODO: review perf here
_gsub.EnableLigation = this.EnableLigature;
_gsub.EnableComposition = this.EnableComposition;
_gsub.EnableComposition = this.EnableComposition;
_gsub.DoSubstitution(glyphs);
}

Expand All @@ -396,12 +419,14 @@ void Layout(GlyphIndexList glyphs)
{
//at this stage _inputGlyphs and _glyphPositions
//has member 1:1
ushort glyIndex, input_codepointOffset, input_mapLen;
glyphs.GetGlyphIndexAndMap(i, out glyIndex, out input_codepointOffset, out input_mapLen);
glyphs.GetGlyphIndexAndMap(i,
out ushort glyphIndex,
out ushort input_codepointOffset,
out ushort input_mapLen);
//
Glyph orgGlyph = _typeface.GetGlyphByIndex(glyIndex);
Glyph orgGlyph = _typeface.GetGlyph(glyphIndex);
//this is original value WITHOUT fit-to-grid adjust
_glyphPositions.AddGlyph(input_codepointOffset, glyIndex, orgGlyph);
_glyphPositions.AddGlyph(input_codepointOffset, glyphIndex, orgGlyph);
}

PositionTechnique posTech = this.PositionTechnique;
Expand Down Expand Up @@ -456,12 +481,12 @@ public void GenerateUnscaledGlyphPlans(IUnscaledGlyphPlanList outputGlyphPlanLis
int finalGlyphCount = glyphPositions.Count;
for (int i = 0; i < finalGlyphCount; ++i)
{
short offsetX, offsetY, advW;

ushort glyphIndex = glyphPositions.GetGlyph(i,
out ushort input_offset,
out offsetX,
out offsetY,
out advW);
out short offsetX,
out short offsetY,
out short advW);
//
outputGlyphPlanList.Append(new UnscaledGlyphPlan(
input_offset,
Expand All @@ -479,12 +504,11 @@ public IEnumerable<UnscaledGlyphPlan> GetUnscaledGlyphPlanIter()
int finalGlyphCount = glyphPositions.Count;
for (int i = 0; i < finalGlyphCount; ++i)
{
short offsetX, offsetY, advW;
ushort glyphIndex = glyphPositions.GetGlyph(i,
out ushort input_offset,
out offsetX,
out offsetY,
out advW);
out short offsetX,
out short offsetY,
out short advW);

yield return new UnscaledGlyphPlan(
input_offset,
Expand Down Expand Up @@ -595,8 +619,6 @@ public void AppendGlyphAdvance(int index, short appendAdvX, short appendAdvY)
pos.advanceW += appendAdvX;//TODO: review for appendY
_glyphPosList[index] = pos;
}


}

struct GlyphPos
Expand Down
5 changes: 2 additions & 3 deletions Typography.GlyphLayout/GlyphPosition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ namespace Typography.TextLayout
class GlyphSetPosition
{

Typeface _typeface;
readonly Typeface _typeface;
GPOS _gposTable;
List<GPOS.LookupTable> _lookupTables;
internal List<GPOS.LookupTable> _lookupTables;
public GlyphSetPosition(Typeface typeface, string lang)
{
this.Lang = lang;
Expand Down Expand Up @@ -69,7 +69,6 @@ public GlyphSetPosition(Typeface typeface, string lang)
}

//-----------------------

_lookupTables = new List<GPOS.LookupTable>();
int j = features.Count;
for (int i = 0; i < j; ++i)
Expand Down
62 changes: 45 additions & 17 deletions Typography.GlyphLayout/GlyphSubstitution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,25 +74,39 @@ public bool EnableComposition

}
}

public bool EnableMathFeature
{
get => _enableMathFeature;
set
{
if (value != _enableMathFeature)
{
_mustRebuildTables = true;
}
_enableMathFeature = value;
}
}
readonly string _language;
bool _enableLigation = true; // enable by default
bool _enableComposition = true;
bool _mustRebuildTables = true;
bool _enableMathFeature = true;

Typeface _typeface;
List<GSUB.LookupTable> _lookupTables = new List<GSUB.LookupTable>();

void RebuildTables()

internal List<GSUB.LookupTable> _lookupTables = new List<GSUB.LookupTable>();

internal void RebuildTables()
{
_lookupTables.Clear();

// check if this lang has
GSUB gsubTable = _typeface.GSUBTable;
ScriptTable scriptTable = gsubTable.ScriptList[_language];
if (scriptTable == null)
{
//no script table for request lang-> no lookup process here
return;
}
if (scriptTable == null) return;


ScriptTable.LangSysTable selectedLang = null;
if (scriptTable.langSysTables != null && scriptTable.langSysTables.Length > 0)
Expand Down Expand Up @@ -120,25 +134,38 @@ void RebuildTables()
foreach (ushort featureIndex in selectedLang.featureIndexList)
{
FeatureList.FeatureTable feature = gsubTable.FeatureList.featureTables[featureIndex];
bool featureIsNeeded = false;
bool includeThisFeature = false;
switch (feature.TagName)
{
case "ccmp": // glyph composition/decomposition
featureIsNeeded = EnableComposition;
includeThisFeature = EnableComposition;
break;
case "liga": // Standard Ligatures --enable by default
featureIsNeeded = EnableLigation;
includeThisFeature = EnableLigation;
break;


//OpenType Layout tags for math processing:
//https://docs.microsoft.com/en-us/typography/opentype/spec/math
//'math', 'ssty','flac','dtls'
case "ssty":
includeThisFeature = EnableMathFeature;
break;
case "dlts"://'dtls' Dotless Forms
includeThisFeature = EnableMathFeature;
break;
case "flac": //Flattened Accents over Capitals
break;
}

if (featureIsNeeded)
if (includeThisFeature)
{
foreach (ushort lookupIndex in feature.LookupListIndices)
{
_lookupTables.Add(gsubTable.LookupList[lookupIndex]);
}
}
}
}
}

/// <summary>
Expand Down Expand Up @@ -189,8 +216,8 @@ public static void CollectAllAssociateGlyphIndex(this Typeface typeface, List<us

//if user dose not specific the unicode lanf bit ranges
//the we try to select it ourself.
UnicodeLangBits[] unicodeLangBitsRanges;
if (ScriptLangs.TryGenUnicodeLangBitsArray(scLang.shortname, out unicodeLangBitsRanges))

if (ScriptLangs.TryGetUnicodeLangBitsArray(scLang.shortname, out UnicodeLangBits[] unicodeLangBitsRanges))
{
//one lang may contains may ranges
if (selectedRangs != null)
Expand All @@ -205,11 +232,12 @@ public static void CollectAllAssociateGlyphIndex(this Typeface typeface, List<us
int endAt = rngInfo.EndAt;
for (int codePoint = rngInfo.StartAt; codePoint <= endAt; ++codePoint)
{
ushort glyghIndex = typeface.LookupIndex(codePoint);
if (glyghIndex > 0)

ushort glyphIndex = typeface.GetGlyphIndex(codePoint);
if (glyphIndex > 0)
{
//add this glyph index
outputGlyphIndexList.Add(glyghIndex);
outputGlyphIndexList.Add(glyphIndex);
}
}
}
Expand Down
Loading

0 comments on commit d712fdb

Please sign in to comment.