Skip to content

Commit

Permalink
Version 0.10.0 Beta.2 [Prerelease]
Browse files Browse the repository at this point in the history
Second beta release of 0.10.0

Co-authored-by: Fribur <[email protected]>
  • Loading branch information
Dreaming381 and Fribur committed May 16, 2024
1 parent 617741f commit a4fd2e5
Show file tree
Hide file tree
Showing 19 changed files with 1,464 additions and 1,018 deletions.
53 changes: 27 additions & 26 deletions Calligraphics/Authoring/BakingSystems/FontSmartBlobberSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,15 @@ public static unsafe BlobAssetReference<FontBlob> BakeFont(FontAsset font, Mater
fontBlobRoot.baseLine = font.faceInfo.baseline;
fontBlobRoot.ascentLine = font.faceInfo.ascentLine;
fontBlobRoot.descentLine = font.faceInfo.descentLine;
fontBlobRoot.capLine = font.faceInfo.capLine;
fontBlobRoot.meanLine = font.faceInfo.meanLine;
fontBlobRoot.lineHeight = font.faceInfo.lineHeight;
fontBlobRoot.subscriptOffset = font.faceInfo.subscriptOffset;
fontBlobRoot.subscriptSize = font.faceInfo.subscriptSize;
fontBlobRoot.superscriptOffset = font.faceInfo.superscriptOffset;
fontBlobRoot.superscriptSize = font.faceInfo.superscriptSize;
fontBlobRoot.capLine = font.faceInfo.capLine;
fontBlobRoot.tabWidth = font.faceInfo.tabWidth;
fontBlobRoot.tabMultiple = font.tabMultiple;
fontBlobRoot.regularStyleSpacing = font.regularStyleSpacing;
fontBlobRoot.regularStyleWeight = font.regularStyleWeight;
fontBlobRoot.boldStyleSpacing = font.boldStyleSpacing;
Expand All @@ -135,49 +138,48 @@ public static unsafe BlobAssetReference<FontBlob> BakeFont(FontAsset font, Mater
hashCounts.Clear();
// Todo: Currently, we allocate a glyph per character and leave characters with null glyphs uninitialized.
// We should rework that to only allocate glyphs to save memory.
BlobBuilderArray<GlyphBlob> glyphBuilder = builder.Allocate(ref fontBlobRoot.characters, font.characterTable.Count);
var characterLookupTable = font.characterLookupTable;
BlobBuilderArray<GlyphBlob> glyphBuilder = builder.Allocate(ref fontBlobRoot.characters, characterLookupTable.Count);
BlobBuilderArray<AdjustmentPair> adjustmentPairs = builder.Allocate(ref fontBlobRoot.adjustmentPairs, glyphPairAdjustmentsSource.Count);

var characterTable = font.characterTable;


for (int i = 0; i < glyphPairAdjustmentsSource.Count; i++)
{
var kerningPair = glyphPairAdjustmentsSource[i];
if (GlyphIndexToUnicode(kerningPair.firstAdjustmentRecord.glyphIndex, characterTable, out int firstUnicode) &&
GlyphIndexToUnicode(kerningPair.secondAdjustmentRecord.glyphIndex, characterTable, out int secondUnicode))
var kerningPair = glyphPairAdjustmentsSource[i];
if (GlyphIndexToUnicode(kerningPair.firstAdjustmentRecord.glyphIndex, characterLookupTable, out int firstUnicode) &&
GlyphIndexToUnicode(kerningPair.secondAdjustmentRecord.glyphIndex, characterLookupTable, out int secondUnicode))
{
adjustmentPairs[i] = new AdjustmentPair
{
firstAdjustment = new GlyphAdjustment
{
xPlacement = kerningPair.firstAdjustmentRecord.glyphValueRecord.xPlacement,
yPlacement = kerningPair.firstAdjustmentRecord.glyphValueRecord.yPlacement,
xAdvance = kerningPair.firstAdjustmentRecord.glyphValueRecord.xAdvance,
yAdvance = kerningPair.firstAdjustmentRecord.glyphValueRecord.yAdvance,
xAdvance = kerningPair.firstAdjustmentRecord.glyphValueRecord.xAdvance,
yAdvance = kerningPair.firstAdjustmentRecord.glyphValueRecord.yAdvance,
},
secondAdjustment = new GlyphAdjustment
{
xPlacement = kerningPair.secondAdjustmentRecord.glyphValueRecord.xPlacement,
yPlacement = kerningPair.secondAdjustmentRecord.glyphValueRecord.yPlacement,
xAdvance = kerningPair.secondAdjustmentRecord.glyphValueRecord.xAdvance,
yAdvance = kerningPair.secondAdjustmentRecord.glyphValueRecord.yAdvance,
xAdvance = kerningPair.secondAdjustmentRecord.glyphValueRecord.xAdvance,
yAdvance = kerningPair.secondAdjustmentRecord.glyphValueRecord.yAdvance,
},
fontFeatureLookupFlags = kerningPair.featureLookupFlags,
firstUnicode = firstUnicode,
secondUnicode = secondUnicode
firstUnicode = firstUnicode,
secondUnicode = secondUnicode
};
}
}

for (int i = 0; i < font.characterTable.Count; i++)
{
var character = font.characterTable[i];
var glyph = character.glyph;
int characterCount = 0;
foreach (var character in characterLookupTable.Values)
{
var glyph = character.glyph;
if (glyph == null)
continue;
var unicode = math.asint(character.unicode);

ref GlyphBlob glyphBlob = ref glyphBuilder[i];
ref GlyphBlob glyphBlob = ref glyphBuilder[characterCount++];

glyphBlob.unicode = unicode;
glyphBlob.glyphScale = glyph.scale;
Expand All @@ -201,7 +203,7 @@ public static unsafe BlobAssetReference<FontBlob> BakeFont(FontAsset font, Mater
for (int j = 0; j < bk.Length; j++)
{
var d = adjustmentCacheBefore[j];
bk[j] = d.x;//unicode
bk[j] = d.x; //unicode
bv[j] = d.y;
}
adjustmentCacheAfter.Sort(new XSorter());
Expand All @@ -210,7 +212,7 @@ public static unsafe BlobAssetReference<FontBlob> BakeFont(FontAsset font, Mater
for (int j = 0; j < ak.Length; j++)
{
var d = adjustmentCacheAfter[j];
ak[j] = d.x;//unicode
ak[j] = d.x; //unicode
av[j] = d.y;
}

Expand Down Expand Up @@ -246,15 +248,14 @@ public static unsafe BlobAssetReference<FontBlob> BakeFont(FontAsset font, Mater

return result;
}
static bool GlyphIndexToUnicode(uint glyphIndex, List<Character> characterTable, out int unicode)
static bool GlyphIndexToUnicode(uint glyphIndex, Dictionary<uint, Character> characterLookupTable, out int unicode)
{
unicode = default;
for (int i = 0, end = characterTable.Count; i < end; i++)
foreach (var character in characterLookupTable.Values)
{
var currentGlyphIndex = characterTable[i].glyphIndex;
if (currentGlyphIndex == glyphIndex)
if (character.glyphIndex == glyphIndex)
{
unicode = math.asint(characterTable[i].unicode);
unicode = math.asint(character.unicode);
return true;
}
}
Expand Down
6 changes: 2 additions & 4 deletions Calligraphics/Authoring/LatiosTextBackendBakingUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ public static void BakeTextBackendMeshAndMaterial(this IBaker baker, Renderer re

var entity = baker.GetEntity(TransformUsageFlags.Renderable);

baker.AddComponent(entity, new TextRenderControl { flags = TextRenderControl.Flags.Dirty });
baker.AddBuffer<RenderGlyph>(entity);
baker.AddComponent( entity, new TextRenderControl { flags = TextRenderControl.Flags.Dirty });
baker.AddComponent<TextShaderIndex>(entity);
}

Expand All @@ -50,8 +49,7 @@ public static void BakeTextBackendMeshAndMaterial(this IBaker baker, MeshRendere

baker.BakeMeshAndMaterial(rendererSettings, mesh, material);

baker.AddComponent(rendererSettings.targetEntity, new TextRenderControl { flags = TextRenderControl.Flags.Dirty });
baker.AddBuffer<RenderGlyph>(rendererSettings.targetEntity);
baker.AddComponent( rendererSettings.targetEntity, new TextRenderControl { flags = TextRenderControl.Flags.Dirty });
baker.AddComponent<TextShaderIndex>(rendererSettings.targetEntity);
}

Expand Down
24 changes: 18 additions & 6 deletions Calligraphics/Authoring/TextRendererAuthoring.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,17 @@ public class TextRendererAuthoring : MonoBehaviour
public bool wordWrap = true;
public float maxLineWidth = float.MaxValue;
public HorizontalAlignmentOptions horizontalAlignment = HorizontalAlignmentOptions.Left;
public VerticalAlignmentOptions verticalAlignment = VerticalAlignmentOptions.Top;
public bool isOrthographic;
public bool enableKerning = true;
public FontStyles fontStyle;
public FontWeight fontWeight;
public VerticalAlignmentOptions verticalAlignment = VerticalAlignmentOptions.TopAscent;
public bool isOrthographic = false;
public bool enableKerning = true;
public FontStyles fontStyle = FontStyles.Normal;
public FontWeight fontWeight = FontWeight.Regular;
[Tooltip("Additional word spacing in font units where a value of 1 equals 1/100em.")]
public float wordSpacing = 0;
[Tooltip("Additional line spacing in font units where a value of 1 equals 1/100em.")]
public float lineSpacing = 0;
[Tooltip("Paragraph spacing in font units where a value of 1 equals 1/100em.")]
public float paragraphSpacing = 0;

public Color32 color = UnityEngine.Color.white;

Expand Down Expand Up @@ -61,17 +67,20 @@ public override void Bake(TextRendererAuthoring authoring)

// Fonts and rendering
AddFontRendering(entity, authoring.fontsAndMaterials[0]);
AddBuffer<RenderGlyph>(entity);
if (authoring.gpuResident)
AddComponent<GpuResidentTextTag>(entity);

if (authoring.fontsAndMaterials.Count > 1)
{
AddComponent<TextMaterialMaskShaderIndex>(entity);
AddBuffer<FontMaterialSelectorForGlyph>(entity);
AddBuffer<RenderGlyphMask>(entity);
AddBuffer<RenderGlyphMask>( entity);
var additionalEntities = AddBuffer<Rendering.AdditionalFontMaterialEntity>(entity).Reinterpret<Entity>();
for (int i = 1; i < authoring.fontsAndMaterials.Count; i++)
{
if (authoring.fontsAndMaterials[i].font == null)
continue;
var newEntity = CreateAdditionalEntity(TransformUsageFlags.Renderable);
AddFontRendering(newEntity, authoring.fontsAndMaterials[i]);
AddComponent<TextMaterialMaskShaderIndex>(newEntity);
Expand All @@ -96,6 +105,9 @@ public override void Bake(TextRendererAuthoring authoring)
enableKerning = authoring.enableKerning,
fontStyle = authoring.fontStyle,
fontWeight = authoring.fontWeight,
wordSpacing = authoring.wordSpacing,
lineSpacing = authoring.lineSpacing,
paragraphSpacing = authoring.paragraphSpacing,
});
}

Expand Down
24 changes: 11 additions & 13 deletions Calligraphics/Components/FontBlob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ public struct FontBlob
public BlobArray<GlyphBlob> characters;
public BlobArray<BlobArray<GlyphLookup> > glyphLookupMap;
public BlobArray<AdjustmentPair> adjustmentPairs;
public float baseLine;
public float ascentLine;
public float descentLine;
public float capLine;
public float meanLine;
public float lineHeight;
public float pointSize;
public float scale;

public float baseLine;
public float atlasWidth;
public float atlasHeight;

Expand All @@ -31,24 +33,20 @@ public struct FontBlob
public float boldStyleWeight;
public byte italicsStyleSlant;

public float capLine;

public float subscriptOffset;
public float subscriptSize;
public float superscriptOffset;
public float superscriptSize;

public float tabWidth;
public float tabMultiple;

//public float underlineOffset;

/// <summary>
/// Padding that is read from material properties
/// </summary>
public float materialPadding;

public float baseScale => 1f / pointSize * scale * .1f;

public const float smallCapsScaleMultiplier = .8f;
public const float orthographicScaleMultiplier = 10f;
}

public struct GlyphBlob
Expand All @@ -64,13 +62,13 @@ public struct GlyphBlob
public struct GlyphLookup
{
public int unicode;
public int index;
public int index;
}

public struct AdjustmentPair
{
public int firstUnicode;
public int secondUnicode;
public int firstUnicode;
public int secondUnicode;
public FontFeatureLookupFlags fontFeatureLookupFlags;
public GlyphAdjustment firstAdjustment;
public GlyphAdjustment secondAdjustment;
Expand All @@ -94,9 +92,9 @@ public struct GlyphAdjustment

public struct AdjustmentPairLookupByUnicode
{
public BlobArray<int> beforeKeys;//unicode
public BlobArray<int> beforeKeys; //unicode
public BlobArray<int> beforeIndices;
public BlobArray<int> afterKeys;//unicode
public BlobArray<int> afterKeys; //unicode
public BlobArray<int> afterIndices;

public unsafe bool TryGetAdjustmentPairIndexForUnicodeBefore(int otherUnicodeBefore, out int index)
Expand Down
50 changes: 33 additions & 17 deletions Calligraphics/Components/TextComponents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public struct FontBlobReference : IComponentData
/// </summary>
public struct TextBaseConfiguration : IComponentData
{
// Todo: Current size is 28 bytes. We could make this 20 bytes by using half for
// fontSize, wordSpacing, lineSpacing, and paragraphSpacing. Worth it?

/// <summary>
/// The size of the font, in font sizes
/// </summary>
Expand All @@ -33,26 +36,30 @@ public struct TextBaseConfiguration : IComponentData
/// The vertex colors of the rendered text
/// </summary>
public Color32 color;

public float wordSpacing;
public float lineSpacing;
public float paragraphSpacing;
/// <summary>
/// The horizontal alignment mode of the text
/// </summary>
public HorizontalAlignmentOptions lineJustification
{
get => (HorizontalAlignmentOptions)((m_alignmentWeightOrtho & 0x70) >> 4);
set => m_alignmentWeightOrtho = (ushort)((m_alignmentWeightOrtho & ~0x70) | ((ushort)value << 4));
get => (HorizontalAlignmentOptions)((m_alignmentWeightOrthoKerning & 0x70) >> 4);
set => m_alignmentWeightOrthoKerning = (ushort)((m_alignmentWeightOrthoKerning & ~0x70) | ((ushort)value << 4));
}
/// <summary>
/// The vertical alignment mode of the text
/// </summary>
public VerticalAlignmentOptions verticalAlignment
{
get => (VerticalAlignmentOptions)((m_alignmentWeightOrtho & 0x380) >> 7);
set => m_alignmentWeightOrtho = (ushort)((m_alignmentWeightOrtho & ~0x380) | ((ushort)value << 7));
get => (VerticalAlignmentOptions)((m_alignmentWeightOrthoKerning & 0x780) >> 7);
set => m_alignmentWeightOrthoKerning = (ushort)((m_alignmentWeightOrthoKerning & ~0x780) | ((ushort)value << 7));
}
public FontWeight fontWeight
{
get => (FontWeight)(m_alignmentWeightOrtho & 0x0f);
set => m_alignmentWeightOrtho = (ushort)((m_alignmentWeightOrtho & ~0x0f) | (ushort)value);
get => (FontWeight)(m_alignmentWeightOrthoKerning & 0x0f);
set => m_alignmentWeightOrthoKerning = (ushort)((m_alignmentWeightOrthoKerning & ~0x0f) | (ushort)value);
}
public FontStyles fontStyle
{
Expand All @@ -61,13 +68,17 @@ public FontStyles fontStyle
}
public bool isOrthographic
{
get => (m_alignmentWeightOrtho & 0x8000) != 0;
set => m_alignmentWeightOrtho = (ushort)((m_alignmentWeightOrtho & 0x7fff) | (value ? 0x8000 : 0));
get => (m_alignmentWeightOrthoKerning & 0x8000) != 0;
set => m_alignmentWeightOrthoKerning = (ushort)((m_alignmentWeightOrthoKerning & 0x7fff) | (value ? 0x8000 : 0));
}
public bool enableKerning
{
get => (m_alignmentWeightOrthoKerning & 0x4000) != 0;
set => m_alignmentWeightOrthoKerning = (ushort)((m_alignmentWeightOrthoKerning & 0xbfff) | (value ? 0x4000 : 0));
}
public bool enableKerning;

private ushort m_fontStyleFlags; // 6 bits unused, but Unity may add more.
ushort m_alignmentWeightOrtho; // 5 bits unused.
ushort m_fontStyleFlags; // 6 bits unused, but Unity may add more.
ushort m_alignmentWeightOrthoKerning; // 3 bits unused.
}

/// <summary>
Expand Down Expand Up @@ -130,12 +141,17 @@ public enum HorizontalAlignmentOptions : byte
/// </summary>
public enum VerticalAlignmentOptions : byte
{
Top,
Middle,
Bottom,
Baseline,
Geometry,
Capline,
TopBase,
TopAscent,
TopDescent,
TopCap,
TopMean,
BottomBase,
BottomAscent,
BottomDescent,
BottomCap,
BottomMean,
MiddleTopAscentToBottomDescent,
}

public enum FontWeight
Expand Down
1 change: 1 addition & 0 deletions Calligraphics/Internal/FixedStack512Bytes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public T RemoveExceptRoot()
Pop();
return Peek();
}
public void Clear() => m_buffer.Clear();
}
}

Loading

0 comments on commit a4fd2e5

Please sign in to comment.