Skip to content

Commit

Permalink
Added: Font fallback structure for the default fonts. (#217)
Browse files Browse the repository at this point in the history
* Added: Font fallback structure for the default fonts.

* Improved: DejaVu Sans Mono as first mono type
Improved: Courier New is more often available than Courier

* Added: Roboto font to Dockerfile
  • Loading branch information
daanggc authored Apr 23, 2024
1 parent 2150dac commit cb12101
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 3 deletions.
90 changes: 87 additions & 3 deletions src/BinaryKits.Zpl.Viewer/ElementDrawers/DrawerOptions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using SkiaSharp;
using System;
using System.Diagnostics;
using System.Linq;

namespace BinaryKits.Zpl.Viewer.ElementDrawers
{
Expand All @@ -21,14 +22,97 @@ public class DrawerOptions

public static Func<string, SKTypeface> DefaultFontLoader = fontName => {
var typeface = SKTypeface.Default;
var skFontManager = SKFontManager.Default;
var fontFamilies = skFontManager.FontFamilies;
if (fontName == "0")
{
//typeface = SKTypeface.FromFile(@"swiss-721-black-bt.ttf");
typeface = SKTypeface.FromFamilyName("Helvetica", SKFontStyleWeight.Bold, SKFontStyleWidth.SemiCondensed, SKFontStyleSlant.Upright);
if (fontFamilies.Contains("Helvetica"))
{
typeface = SKTypeface.FromFamilyName(
"Helvetica",
SKFontStyleWeight.Bold,
SKFontStyleWidth.SemiCondensed,
SKFontStyleSlant.Upright
);
}
else if (fontFamilies.Contains("Roboto Condensed"))
{
typeface = SKTypeface.FromFamilyName(
"Roboto Condensed",
SKFontStyleWeight.Bold,
SKFontStyleWidth.Normal,
SKFontStyleSlant.Upright
);
}
else if (fontFamilies.Contains("Swis721 BT"))
{
//Note: Zebra Swis721 BT (tt0003m_.ttf) is not as bold and condensed as Labelary
//Note: swiss-721-bt-bold.ttf is not as condensed as Labelary
//typeface = SKTypeface.FromFile(@"swiss-721-black-bt.ttf");
typeface = SKTypeface.FromFamilyName(
"Swis721 BT"
);
}
else if (fontFamilies.Contains("Arial"))
{
typeface = SKTypeface.FromFamilyName(
"Arial",
SKFontStyleWeight.Bold,
SKFontStyleWidth.Condensed,
SKFontStyleSlant.Upright
);
}
else
{
//let the system provide a fallback for Helvetica
typeface = SKTypeface.FromFamilyName(
"Helvetica",
SKFontStyleWeight.Bold,
SKFontStyleWidth.SemiCondensed,
SKFontStyleSlant.Upright
);
}
}
else
{
typeface = SKTypeface.FromFamilyName("DejaVu Sans Mono", SKFontStyleWeight.Normal, SKFontStyleWidth.Normal, SKFontStyleSlant.Upright);
if (fontFamilies.Contains("DejaVu Sans Mono"))
{
typeface = SKTypeface.FromFamilyName(
"DejaVu Sans Mono",
SKFontStyleWeight.Normal,
SKFontStyleWidth.Normal,
SKFontStyleSlant.Upright
);
}
else if (fontFamilies.Contains("Courier New"))
{
typeface = SKTypeface.FromFamilyName(
"Courier New",
SKFontStyleWeight.Medium,
SKFontStyleWidth.Normal,
SKFontStyleSlant.Upright
);
}
else if (fontFamilies.Contains("Courier"))
{
typeface = SKTypeface.FromFamilyName(
"Courier",
SKFontStyleWeight.Medium,
SKFontStyleWidth.Normal,
SKFontStyleSlant.Upright
);
}
else
{
//let the system provide a fallback for DejaVu
typeface = SKTypeface.FromFamilyName(
"DejaVu Sans Mono",
SKFontStyleWeight.Normal,
SKFontStyleWidth.Normal,
SKFontStyleSlant.Upright
);
}
}
return typeface;
Expand Down
1 change: 1 addition & 0 deletions src/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ RUN apt-get update \
libc6-dev \
libgdiplus \
libx11-dev \
fonts-roboto \
&& rm -rf /var/lib/apt/lists/*

WORKDIR /app
Expand Down

0 comments on commit cb12101

Please sign in to comment.