Created
February 21, 2022 18:53
-
-
Save 06b/c52b7638085aa5f56cb0c31fea188555 to your computer and use it in GitHub Desktop.
Extension method for adding soft hyphens.
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
using System; | |
using System.Text; | |
namespace Typography.Extensions | |
{ | |
/// <summary> | |
/// Contains extension methods for adding soft hyphens. | |
/// </summary> | |
public class SoftHyphenHelperExtensions | |
{ | |
private const string SoftHyphen = "­"; | |
//Support: Typography Hyphenation | |
//Details: While automatic hyphenation on the web has been possible since 2011, support for auto css hyphenation | |
// still is lacking, thus by adding suggested line break opportunities such as invisible "soft" hyphens, | |
// will override browser automatic break point, providing appropriate hyphenation. | |
//Supporting Details: https://caniuse.com/css-hyphens | |
//Additional Details: http://clagnut.com/blog/2395 | |
//Additional Details: https://jkorpela.fi/shy.html | |
//Additional Details: https://developer.mozilla.org/en-US/docs/Web/CSS/hyphens | |
//Chromium Bug: https://bugs.chromium.org/p/chromium/issues/detail?id=652964 | |
// A common rule of thumb is to only allow words at least six-letters long to be hyphenated - http://clagnut.com/blog/2395 | |
private const int HyphenateCharacterLimit = 6; | |
// The Oxford Style Manual recommends that three 3 is the minimum number of letters after a hyphen at a line break - http://clagnut.com/blog/2395 | |
private const int HyphenateAfterCharacterLimit = 3; | |
/// <summary> | |
/// Converts text to have soft hyphens due to lack of support with the CSS Hyphenation property. | |
/// </summary> | |
/// <param name="title">String which lacks soft hyphens.</param> | |
/// <returns>Title with soft hyphens.</returns> | |
public static string Hyphenate(string title) | |
{ | |
string[] TitleArray = title.Split(" "); | |
int TitleArraySize = TitleArray.Length; | |
StringBuilder SoftHyphenTitle = new StringBuilder(); | |
string ArrayItem; | |
int ArrayItemLength; | |
for (int index = 0; index < TitleArraySize; index++) | |
{ | |
ArrayItem = TitleArray[index]; | |
ArrayItemLength = ArrayItem.Length; | |
if (ArrayItemLength >= HyphenateCharacterLimit) | |
{ | |
for (int softHyphenIndex = 0; softHyphenIndex < ArrayItemLength; softHyphenIndex += HyphenateAfterCharacterLimit) | |
{ | |
SoftHyphenTitle.Append(ArrayItem.Substring(softHyphenIndex, Math.Min(HyphenateAfterCharacterLimit, ArrayItemLength - softHyphenIndex))); | |
if (softHyphenIndex + HyphenateAfterCharacterLimit < ArrayItemLength) | |
{ | |
SoftHyphenTitle.Append(SoftHyphen); | |
} | |
} | |
} | |
else | |
{ | |
SoftHyphenTitle.Append(ArrayItem); | |
} | |
if (index < TitleArraySize - 1) | |
{ | |
SoftHyphenTitle.Append(" "); | |
} | |
} | |
return SoftHyphenTitle.ToString(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment