Skip to content

Commit

Permalink
[Fonts] Remove Rendering related code
Browse files Browse the repository at this point in the history
hyazinthh committed Aug 1, 2024
1 parent 8d21acd commit d43ccc6
Showing 13 changed files with 1,036 additions and 1,120 deletions.
145 changes: 72 additions & 73 deletions src/Aardvark.Base.Fonts/BvhInternal.fs

Large diffs are not rendered by default.

334 changes: 67 additions & 267 deletions src/Aardvark.Base.Fonts/Font.fs

Large diffs are not rendered by default.

145 changes: 71 additions & 74 deletions src/Aardvark.Base.Fonts/FontResolver.fs
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
namespace Aardvark.Rendering.Text
namespace Aardvark.Base.Fonts

open Aardvark.Base
open System
open System.IO
open System.Runtime.InteropServices
open Microsoft.FSharp.NativeInterop
open System.Security
open FuzzySharp
open Typography.OpenFont

#nowarn "9"

module internal FontResolver =

type FontTableEntry<'a> =
{
Tag : 'a
@@ -22,7 +19,7 @@ module internal FontResolver =
Italic : bool
SubFamilyName : string
}

module FontTableEntries =
// resolve according to: https://developer.mozilla.org/en-US/docs/Web/CSS/font-weight
let chooseBestEntry (weight : int) (italic : bool) (available : list<FontTableEntry<'a>>) =
@@ -38,11 +35,11 @@ module internal FontResolver =
match nonitalic with
| [] -> available
| _ -> nonitalic

let bestEntry =
let map = members |> List.map (fun m -> m.Weight, m) |> MapExt.ofList
let (l, s, r) = MapExt.neighbours weight map

match s with
| Some (_, m) -> m
| None ->
@@ -60,21 +57,21 @@ module internal FontResolver =
| None ->
// If no match is found, look for available weights greater than 500, in ascending order.
Option.get r |> snd

elif weight < 400 then
// If a weight less than 400 is given, look for available weights less than the target, in descending order.
// If no match is found, look for available weights greater than the target, in ascending order.
match l with
| Some (_, lm) -> lm
| None -> Option.get r |> snd

else
// If a weight greater than 500 is given, look for available weights greater than the target, in ascending order.
// If no match is found, look for available weights less than the target, in descending order.
match r with
| Some (_, rm) -> rm
| None -> Option.get l |> snd

bestEntry

let ofStream (tag : 'a) (openStream : unit -> #Stream) =
@@ -86,9 +83,9 @@ module internal FontResolver =
Offset = info.ActualStreamOffset
Weight = int info.Weight
Italic = info.OS2TranslatedStyle.HasFlag Extensions.TranslatedOS2FontStyle.ITALIC || info.OS2TranslatedStyle.HasFlag Extensions.TranslatedOS2FontStyle.OBLIQUE
SubFamilyName = info.SubFamilyName
SubFamilyName = info.SubFamilyName
}

let r = OpenFontReader()
use s = openStream() :> System.IO.Stream
let info = r.ReadPreview s
@@ -98,56 +95,56 @@ module internal FontResolver =
[ofInfo info]
with _ ->
[]

let ofFile (file : string) =
if System.IO.File.Exists file then
ofStream file (fun () -> System.IO.File.OpenRead file)
else
[]

let read (openStream : 'a -> #System.IO.Stream) (entry : FontTableEntry<'a>) =
let reader = OpenFontReader()
use s = openStream entry.Tag :> System.IO.Stream
reader.Read(s, entry.Offset, ReadFlags.Full)


type FontTable<'a> (entries : seq<FontTableEntry<'a>>) =

static let normalizeFamilyName (name : string) =
name.ToLowerInvariant().Trim()



let table =
let dict = System.Collections.Generic.Dictionary<string, list<_>>()

for e in entries do
if not (isNull e.FamilyName) then
let key = normalizeFamilyName e.FamilyName

match dict.TryGetValue key with
| (true, s) ->
dict.[key] <- e :: s
| _ ->
dict.[key] <- [e]

dict

let keys =
table
|> Seq.collect (fun (KeyValue(key, e)) -> e |> Seq.map (fun e -> e.FamilyName, key))
|> Seq.toArray

let names =
keys |> Array.map fst


member x.Find(family : string, weight : int, italic : bool) =
let res = FuzzySharp.Process.ExtractOne(family, names)
let (_, key) = keys.[res.Index]
let entries = table.[key]
FontTableEntries.chooseBestEntry weight italic entries

module private Win32 =
open System.Runtime.InteropServices

@@ -194,7 +191,7 @@ module internal FontResolver =
let mutable run = true

let nameRx = System.Text.RegularExpressions.Regex(@"^(.*?)[ \t]*(Bold Italic|Light Italic|Thin Italic|Bold|Semibold|Thin|Italic|Light)?[ \t]*\([ \t]*(TrueType|OpenType)[ \t]*\)[ \t]*$", System.Text.RegularExpressions.RegexOptions.IgnoreCase)

let result = System.Collections.Generic.List<_>()
let fonts = Environment.GetFolderPath Environment.SpecialFolder.Fonts
while run do
@@ -207,14 +204,14 @@ module internal FontResolver =

use pValue = fixed valueBuffer
use pName = fixed nameBuffer



let ret = RegEnumValue(key, index, pName, &nameLen, 0n, 0n, pValue, &valueLen)
if ret = 0 then
let name = System.Text.Encoding.ASCII.GetString(nameBuffer, 0, nameLen).Trim(' ', char 0)
let file = System.Text.Encoding.ASCII.GetString(valueBuffer, 0, valueLen).Trim(' ', char 0)

let familyName =
let m = nameRx.Match name
if m.Success then
@@ -223,7 +220,7 @@ module internal FontResolver =
else Some value
else
None

let path = Path.Combine(fonts, file)
if File.Exists path then
let entries = FontTableEntries.ofFile path
@@ -232,12 +229,12 @@ module internal FontResolver =
for r in entries do result.Add { r with FamilyName = f }
| _ ->
result.AddRange entries


index <- index + 1
else
run <- false

FontTable result
)

@@ -257,14 +254,14 @@ module internal FontResolver =

[<Literal>]
let private CoreGraphics = "/System/Library/Frameworks/CoreGraphics.framework/Versions/A/CoreGraphics"

[<Literal>]
let private CoreText = "/System/Library/Frameworks/CoreText.framework/Versions/A/CoreText"


[<Struct; StructLayout(LayoutKind.Sequential)>]
type CFRange = { Start : nativeint; Length : nativeint }

[<DllImport(CoreFoundation)>]
extern void* CFDictionaryCreateMutable (void* a, int cap, void* b, void* c)

@@ -301,83 +298,83 @@ module internal FontResolver =
let NSFontFamilyAttribute = CFStringCreateWithCString(0n, "NSFontFamilyAttribute", 0n)
let NSFontFaceAttribute = CFStringCreateWithCString(0n, "NSFontFaceAttribute", 0n)
let NSCTFontFileURLAttribute = CFStringCreateWithCString(0n, "NSCTFontFileURLAttribute", 0n)



let ptr = CFDictionaryCreateMutable (0n, 100000, 0n, 0n)


let coll = CTFontCollectionCreateFromAvailableFonts ptr
let arr = CTFontCollectionCreateMatchingFontDescriptors coll
let cnt = CFArrayGetCount arr

let mutable range = { Start = 0n; Length = nativeint cnt }

let getString (font : nativeint) (att : nativeint) =
let test = CTFontDescriptorCopyAttribute(font, att)
let buffer = Array.zeroCreate<byte> 8192
use ptr = fixed buffer
CFStringGetCString(test, ptr, 8192, 0n)


let mutable l = 0
while l < buffer.Length && buffer.[l] <> 0uy do l <- l + 1



System.Text.Encoding.UTF8.GetString(buffer,0, l)

let getPath (font : nativeint) (att : nativeint) =
let test = CTFontDescriptorCopyAttribute(font, att)
let path = CFURLCopyFileSystemPath(test, 0)

let buffer = Array.zeroCreate<byte> 8192
use ptr = fixed buffer
CFStringGetCString(path, ptr, 8192, 0n)

let mutable l = 0
while l < buffer.Length && buffer.[l] <> 0uy do l <- l + 1


System.Text.Encoding.UTF8.GetString(buffer, 0, l)

let files = System.Collections.Generic.Dictionary<string, System.Collections.Generic.HashSet<string>>()
let func =
CFArrayCallbackDelegate(fun ptr _ ->
let face = getString ptr NSFontFaceAttribute
let family = getString ptr NSFontFamilyAttribute
let path = getPath ptr NSCTFontFileURLAttribute

match files.TryGetValue family with
| (true, set) -> set.Add path |> ignore
| _ ->
let set = System.Collections.Generic.HashSet<string>()
set.Add path |> ignore
files.[family] <- set


)
CFArrayApplyFunction(arr, range, func, 0n)



let allEntries = System.Collections.Generic.List()
for KeyValue(family, files) in files do
for f in files do

let entries = FontTableEntries.ofFile f |> List.map (fun i -> { i with FamilyName = family })
allEntries.AddRange entries


FontTable allEntries
)




let tryLoadTypeFace (family : string) (weight : int) (italic : bool) : Option<Typography.OpenFont.Typeface * string * int * bool> =
try
let entry =
let entry =
match Environment.OSVersion with
| Windows ->
Win32.table.Value.Find(family, weight, italic) |> Some
Loading

0 comments on commit d43ccc6

Please sign in to comment.