Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
4f9f992
Initial implementation
JimBobSquarePants Mar 2, 2022
b04fcaf
Merge branch 'main' into js/text-runs
JimBobSquarePants Mar 2, 2022
2ceb25a
Fix bool assignment
JimBobSquarePants Mar 2, 2022
2ef2353
Trim down to a max of two runs
JimBobSquarePants Mar 2, 2022
65d8384
Ensure runs cannot overlap
JimBobSquarePants Mar 2, 2022
7b74864
Validate run Start and End properties before slicing.
JimBobSquarePants Mar 2, 2022
a4a0e4d
Add some TextRun tests, simplify TextLayout
JimBobSquarePants Mar 3, 2022
3796290
Assert Font is set
JimBobSquarePants Mar 3, 2022
e2b7bd5
Persist TextRun font sizes
JimBobSquarePants Mar 3, 2022
3b62bab
Do groundwork for attribute support.
JimBobSquarePants Mar 4, 2022
9283316
Fix tests build
JimBobSquarePants Mar 4, 2022
ac767a8
First pass at POST table
Redth Mar 4, 2022
f05c44e
Merge pull request #252 from Redth/main
JimBobSquarePants Mar 4, 2022
9790ad6
Wire up post table
JimBobSquarePants Mar 4, 2022
afa057d
Add TODO: notes
JimBobSquarePants Mar 4, 2022
45b5c9c
Superscript/Subscript scaling works.
JimBobSquarePants Mar 4, 2022
7107da3
Scale and offset subscript + superscript
JimBobSquarePants Mar 4, 2022
91916c7
Add TextRun available to GlyphRendererParameters
JimBobSquarePants Mar 4, 2022
3439a98
Optimize scaled transform to a single call
JimBobSquarePants Mar 4, 2022
af80bc8
Restore good API
JimBobSquarePants Mar 4, 2022
a51f4fa
Render underlines/strikeout parts.
tocsoft Mar 4, 2022
c34b1c2
add attributes
tocsoft Mar 4, 2022
74512e3
use better left hand side
tocsoft Mar 4, 2022
a1441e9
drop commented code
tocsoft Mar 4, 2022
1eb941a
Merge pull request #254 from SixLabors/sw/text-run-lines
JimBobSquarePants Mar 4, 2022
f256682
Fix scaled line offset, lockdown glyph rendering, fix bidi tests
JimBobSquarePants Mar 5, 2022
f3de9c9
Fix null check and optimize
JimBobSquarePants Mar 5, 2022
09654f9
better position underline/strikeout with better thinkness
tocsoft Mar 5, 2022
953fb9b
Fix line and layout rendering of Arabic scripts.
JimBobSquarePants Mar 5, 2022
5a09926
Merge branch 'js/text-runs' of https://github.com/SixLabors/Fonts int…
JimBobSquarePants Mar 5, 2022
4b63c79
Update Program.cs
JimBobSquarePants Mar 5, 2022
513d035
Fix overlong stroke
JimBobSquarePants Mar 6, 2022
a75eb02
Don't use rsb for fake advance measurements
JimBobSquarePants Mar 6, 2022
b002415
Always Ceil string measuring results.
JimBobSquarePants Mar 6, 2022
d30b285
Pixel perfect stroke. (Tested with drawing PR)
JimBobSquarePants Mar 6, 2022
205cb94
Split TextDecoration and TextAttribute and add decoration renderer
tocsoft Mar 6, 2022
9889bde
stylecoped
tocsoft Mar 6, 2022
8360ce7
Offset overline to avoid clipping.
JimBobSquarePants Mar 7, 2022
847c668
Fixed fix (ha!)
JimBobSquarePants Mar 7, 2022
1303b7c
Add font metrics tests for new properties
JimBobSquarePants Mar 7, 2022
5d8fdfe
Update enum names to match runtime standard
JimBobSquarePants Mar 7, 2022
642999d
Fix horizontal variable font size line-height handling
JimBobSquarePants Mar 11, 2022
41687c7
Fix initial vertical y offset
JimBobSquarePants Mar 11, 2022
ceb5656
Fix vertical layout + linebreak
JimBobSquarePants Mar 11, 2022
d7b4326
Update test to match fixes
JimBobSquarePants Mar 11, 2022
db51f98
Drop precision for .NET FX
JimBobSquarePants Mar 13, 2022
6a883af
Fix line height
JimBobSquarePants Mar 14, 2022
a43a592
layout box is a fools errand. Leading FTW
JimBobSquarePants Mar 14, 2022
39773e6
Use graphemes, fix line-gap usage, fix mixed line-height
JimBobSquarePants Mar 20, 2022
cfec9a5
Tweak adjusted advance ratio based on testing
JimBobSquarePants Mar 21, 2022
6c3d00b
Fix font hinting
JimBobSquarePants Mar 24, 2022
7b3ec08
Don't upscale already scaled ppem
JimBobSquarePants Mar 26, 2022
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
18 changes: 16 additions & 2 deletions samples/DrawWithImageSharp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ public static void Main(string[] args)
FontFamily arabicFont = SystemFonts.Get("Dubai");

FontFamily tahoma = SystemFonts.Get("Tahoma");
var textRuns = new List<TextRun>
{
new TextRun { Start = 4, End = 9, Font = uiFont.CreateFont(32, FontStyle.Bold) },
new TextRun { Start = 26, End = 30, Font = uiFont.CreateFont(32, FontStyle.Italic) }
};

RenderText(uiFont, "The quick brown fox jumps over the lazy dog", 32, textRuns: textRuns);
return;
RenderText(font2, "\uFB01", pointSize: 11.25F);
RenderText(fontWoff2, "\uFB01", pointSize: 11.25F);
RenderText(tahoma, "p", pointSize: 11.25F);
Expand Down Expand Up @@ -182,12 +190,18 @@ public static void RenderText(TextOptions options, string text)
SaveImage(options, text, (int)size.Width, (int)size.Height, options.Font.Name, text + ".png");
}

public static void RenderText(FontFamily font, string text, float pointSize = 12, IEnumerable<FontFamily> fallbackFonts = null)
public static void RenderText(
FontFamily font,
string text,
float pointSize = 12,
IEnumerable<FontFamily> fallbackFonts = null,
IEnumerable<TextRun> textRuns = null)
=> RenderText(
new TextOptions(new Font(font, pointSize))
{
WrappingLength = 400,
FallbackFontFamilies = fallbackFonts?.ToArray()
FallbackFontFamilies = fallbackFonts?.ToArray(),
TextRuns = textRuns?.ToArray()
},
text);

Expand Down
19 changes: 10 additions & 9 deletions src/SixLabors.Fonts/GlyphPositioningCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,14 @@ public bool TryGetGlyphMetricsAtOffset(int offset, [NotNullWhen(true)] out Glyph
=> this.map.TryGetValue(offset, out metrics);

/// <summary>
/// Adds the collection of glyph ids to the metrics collection.
/// Adding subsequent collections will overwrite any glyphs that have been previously
/// Updates the collection of glyph ids to the metrics collection to overwrite any glyphs that have been previously
/// identified as fallbacks.
/// </summary>
/// <param name="fontMetrics">The font face with metrics.</param>
/// <param name="collection">The glyph substitution collection.</param>
/// <returns><see langword="true"/> if the metrics collection does not contain any fallbacks; otherwise <see langword="false"/>.</returns>
public bool TryAddOrUpdate(FontMetrics fontMetrics, GlyphSubstitutionCollection collection)
public bool TryUpdate(FontMetrics fontMetrics, GlyphSubstitutionCollection collection)
{
if (this.Count == 0)
{
return this.Add(fontMetrics, collection);
}

bool hasFallBacks = false;
List<int> orphans = new();
for (int i = 0; i < this.offsets.Count; i++)
Expand Down Expand Up @@ -163,7 +157,14 @@ public bool TryAddOrUpdate(FontMetrics fontMetrics, GlyphSubstitutionCollection
return !hasFallBacks;
}

private bool Add(FontMetrics fontMetrics, GlyphSubstitutionCollection collection)
/// <summary>
/// Adds the collection of glyph ids to the metrics collection.
/// identified as fallbacks.
/// </summary>
/// <param name="fontMetrics">The font face with metrics.</param>
/// <param name="collection">The glyph substitution collection.</param>
/// <returns><see langword="true"/> if the metrics collection does not contain any fallbacks; otherwise <see langword="false"/>.</returns>
public bool TryAdd(FontMetrics fontMetrics, GlyphSubstitutionCollection collection)
{
bool hasFallBacks = false;
for (int i = 0; i < collection.Count; i++)
Expand Down
6 changes: 2 additions & 4 deletions src/SixLabors.Fonts/GlyphSubstitutionCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public GlyphSubstitutionCollection(TextOptions textOptions)
public int Count => this.offsets.Count;

/// <inheritdoc />
public bool IsVerticalLayoutMode { get; private set; }
public bool IsVerticalLayoutMode { get; }

/// <inheritdoc />
public TextOptions TextOptions { get; }
Expand Down Expand Up @@ -109,13 +109,11 @@ public void AddGlyph(ushort glyphId, CodePoint codePoint, TextDirection directio
/// <summary>
/// Removes all elements from the collection.
/// </summary>
/// <param name="mode">The text layout mode.</param>
public void Clear(LayoutMode mode)
public void Clear()
{
this.offsets.Clear();
this.glyphs.Clear();
this.LigatureId = 1;
this.IsVerticalLayoutMode = mode.IsVertical();
}

/// <summary>
Expand Down
39 changes: 39 additions & 0 deletions src/SixLabors.Fonts/TextAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.

using System;

namespace SixLabors.Fonts
{
/// <summary>
/// Provides enumeration of various text attributes.
/// </summary>
[Flags]
public enum TextAttribute
{
/// <summary>
/// No attributes are applied
/// </summary>
None = 0,

/// <summary>
/// The text is underlined
/// </summary>
Underline = 1 << 0,

/// <summary>
/// The text contains a horizontal line through the center.
/// </summary>
Strikethrough = 1 << 1,

/// <summary>
/// The text set slightly below the normal line of type.
/// </summary>
Subscript = 1 << 2,

/// <summary>
/// The text set slightly above the normal line of type.
/// </summary>
Superscript = 1 << 3,
}
}
137 changes: 98 additions & 39 deletions src/SixLabors.Fonts/TextLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,6 @@ public IReadOnlyList<GlyphLayout> GenerateLayout(ReadOnlySpan<char> text, TextOp
return Array.Empty<GlyphLayout>();
}

if (options.WrappingLength > 0)
{
// Trim trailing white spaces from the text
text = text.TrimEnd(null);
}

// Check our string again after trimming.
if (text.IsEmpty)
{
return Array.Empty<GlyphLayout>();
}

TextBox textBox = ProcessText(text, options);
return LayoutText(textBox, options);
}
Expand All @@ -45,17 +33,9 @@ private static TextBox ProcessText(ReadOnlySpan<char> text, TextOptions options)
{
// Gather the font and fallbacks.
FontMetrics mainFont = options.Font.FontMetrics;
FontMetrics[] fallbackFonts;
if (options.FallbackFontFamilies?.Count > 0)
{
fallbackFonts = options.FallbackFontFamilies
.Select(x => new Font(x, options.Font.Size, options.Font.RequestedStyle).FontMetrics)
.ToArray();
}
else
{
fallbackFonts = Array.Empty<FontMetrics>();
}
FontMetrics[] fallbackFonts = (options.FallbackFontFamilies?.Count > 0)
? options.FallbackFontFamilies.Select(x => new Font(x, options.Font.Size, options.Font.RequestedStyle).FontMetrics).ToArray()
: Array.Empty<FontMetrics>();

LayoutMode layoutMode = options.LayoutMode;
GlyphSubstitutionCollection substitutions = new(options);
Expand All @@ -82,23 +62,36 @@ private static TextBox ProcessText(ReadOnlySpan<char> text, TextOptions options)
Dictionary<int, int> bidiMap = new();

// Incrementally build out collection of glyphs.
// For each run we start with a fresh substitution collection to avoid
// overwriting the glyph ids.
if (!DoFontRun(
text,
options,
mainFont,
bidiRuns,
bidiMap,
substitutions,
positionings))
IReadOnlyList<TextRun> textRuns = BuildTextRuns(text, options);

// First do multiple font runs using the individual text runs.
bool complete = true;
foreach (TextRun textRun in textRuns)
{
if (!DoFontRun(
textRun.Slice(text),
textRun.Start,
false,
textRun.Font!.FontMetrics,
bidiRuns,
bidiMap,
substitutions,
positionings))
{
complete = false;
}
}

if (!complete)
{
// Finally try our fallback fonts.
// We do a complete run here across the whole collection.
foreach (FontMetrics font in fallbackFonts)
{
substitutions.Clear(layoutMode);
if (DoFontRun(
text,
options,
0,
true,
font,
bidiRuns,
bidiMap,
Expand All @@ -113,7 +106,11 @@ private static TextBox ProcessText(ReadOnlySpan<char> text, TextOptions options)
// Update the positions of the glyphs in the completed collection.
// Each set of metrics is associated with single font and will only be updated
// by that font so it's safe to use a single collection.
mainFont.UpdatePositions(positionings);
foreach (TextRun textRun in textRuns)
{
textRun.Font!.FontMetrics.UpdatePositions(positionings);
}

foreach (FontMetrics font in fallbackFonts)
{
font.UpdatePositions(positionings);
Expand Down Expand Up @@ -388,18 +385,78 @@ private static IEnumerable<GlyphLayout> LayoutLineVertical(
return glyphs;
}

private static IReadOnlyList<TextRun> BuildTextRuns(ReadOnlySpan<char> text, TextOptions options)
{
if (options.TextRuns?.Count == 0)
{
return new TextRun[]
{
new()
{
Start = 0,
End = CodePoint.GetCodePointCount(text),
Font = options.Font
}
};
}

int start = 0;
int end = CodePoint.GetCodePointCount(text);
List<TextRun> textRuns = new();
foreach (TextRun textRun in options.TextRuns!.OrderBy(x => x.Start))
{
Comment thread
JimBobSquarePants marked this conversation as resolved.
Resolved
// Fill gaps within runs.
if (textRun.Start > start)
{
textRuns.Add(new()
{
Start = start,
End = textRun.Start,
Font = options.Font
});
}

// Add the current run, ensuring the font is not null.
if (textRun.Font is null)
{
textRun.Font = options.Font;
}

textRuns.Add(textRun);
start = textRun.End;
}

// Add a final run.
if (start < end)
{
textRuns.Add(new()
{
Start = start,
End = end,
Font = options.Font
});
}

return textRuns;
}

private static bool DoFontRun(
ReadOnlySpan<char> text,
TextOptions options,
int start,
bool isFallbackRun,
FontMetrics fontMetrics,
BidiRun[] bidiRuns,
Dictionary<int, int> bidiMap,
GlyphSubstitutionCollection substitutions,
GlyphPositioningCollection positionings)
{
// For each run we start with a fresh substitution collection to avoid
// overwriting the glyph ids.
substitutions.Clear();

// Enumerate through each grapheme in the text.
int graphemeIndex;
int codePointIndex = 0;
int codePointIndex = start;
int bidiRun = 0;
var graphemeEnumerator = new SpanGraphemeEnumerator(text);
for (graphemeIndex = 0; graphemeEnumerator.MoveNext(); graphemeIndex++)
Expand Down Expand Up @@ -450,7 +507,9 @@ private static bool DoFontRun(
SubstituteBidiMirrors(fontMetrics, substitutions);
fontMetrics.ApplySubstitution(substitutions);

return positionings.TryAddOrUpdate(fontMetrics, substitutions);
return !isFallbackRun
? positionings.TryAdd(fontMetrics, substitutions)
: positionings.TryUpdate(fontMetrics, substitutions);
}

private static void SubstituteBidiMirrors(FontMetrics fontMetrics, GlyphSubstitutionCollection collection)
Expand Down
10 changes: 8 additions & 2 deletions src/SixLabors.Fonts/TextOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
namespace SixLabors.Fonts
{
/// <summary>
/// Provides configuration options for rendering text.
/// Provides configuration options for rendering and shaping text.
/// </summary>
public sealed class TextOptions
public class TextOptions
Comment thread
JimBobSquarePants marked this conversation as resolved.
Resolved
{
private float tabWidth = 4F;
private float dpi = 72F;
Expand Down Expand Up @@ -48,6 +48,7 @@ public TextOptions(TextOptions options)
this.KerningMode = options.KerningMode;
this.ColorFontSupport = options.ColorFontSupport;
this.FeatureTags = new List<Tag>(options.FeatureTags);
this.TextRuns = new List<TextRun>(options.TextRuns);
}

/// <summary>
Expand Down Expand Up @@ -181,5 +182,10 @@ public float LineSpacing
/// Gets or sets the collection of additional feature tags to apply during glyph shaping.
/// </summary>
public IReadOnlyList<Tag> FeatureTags { get; set; } = Array.Empty<Tag>();

/// <summary>
/// Gets or sets an optional collection of text runs to apply to the body of text.
/// </summary>
public IReadOnlyList<TextRun> TextRuns { get; set; } = Array.Empty<TextRun>();
Comment thread
JimBobSquarePants marked this conversation as resolved.
Resolved
}
}
Loading