From a2af81994c84aa709c6ee75176d31156a41091e3 Mon Sep 17 00:00:00 2001 From: Dany LE Date: Tue, 1 Mar 2022 19:22:26 +0100 Subject: [PATCH] Add freetype font manager --- Diya/DiyaBoot.class.st | 1 + Diya/DiyaFontFamily.class.st | 47 ++++++++++++++++++ Diya/DiyaFontManager.class.st | 90 +++++++++++++++++++++++++++++++++++ 3 files changed, 138 insertions(+) create mode 100644 Diya/DiyaFontFamily.class.st create mode 100644 Diya/DiyaFontManager.class.st diff --git a/Diya/DiyaBoot.class.st b/Diya/DiyaBoot.class.st index ffd85d2..0ab735f 100644 --- a/Diya/DiyaBoot.class.st +++ b/Diya/DiyaBoot.class.st @@ -214,6 +214,7 @@ DiyaBoot >> startx [ context delete. renderer destroy. window destroy. + DiyaFontManager reset. SDL2 quit. ] diff --git a/Diya/DiyaFontFamily.class.st b/Diya/DiyaFontFamily.class.st new file mode 100644 index 0000000..152d761 --- /dev/null +++ b/Diya/DiyaFontFamily.class.st @@ -0,0 +1,47 @@ +Class { + #name : #DiyaFontFamily, + #superclass : #DiyaBaseObject, + #instVars : [ + 'faces', + 'name' + ], + #category : #'Diya-Fonts' +} + +{ #category : #'instance creation' } +DiyaFontFamily class >> fromFace: face [ + ^self new addFace: face; yourself. +] + +{ #category : #accessing } +DiyaFontFamily >> addFace: face [ + name ifNil: [ name := face familyName ]. + faces at: face styleName ifAbsentPut: face. +] + +{ #category : #accessing } +DiyaFontFamily >> faces [ + ^ faces +] + +{ #category : #initialization } +DiyaFontFamily >> initialize [ + super initialize. + faces := Dictionary new. + name := nil +] + +{ #category : #accessing } +DiyaFontFamily >> name [ + ^ name +] + +{ #category : #accessing } +DiyaFontFamily >> name: anObject [ + name := anObject +] + +{ #category : #accessing } +DiyaFontFamily >> styleNames [ + ^faces keys +] diff --git a/Diya/DiyaFontManager.class.st b/Diya/DiyaFontManager.class.st new file mode 100644 index 0000000..8a77989 --- /dev/null +++ b/Diya/DiyaFontManager.class.st @@ -0,0 +1,90 @@ +Class { + #name : #DiyaFontManager, + #superclass : #DiyaSingleton, + #instVars : [ + 'families' + ], + #category : #'Diya-Fonts' +} + +{ #category : #'instance creation' } +DiyaFontManager class >> cleanUpInstance: singleton [ + singleton reset. +] + +{ #category : #'class initialization' } +DiyaFontManager class >> initialize [ + self uniqueInstance reset. +] + +{ #category : #'as yet unclassified' } +DiyaFontManager class >> searchPaths [ + ^ { + Smalltalk imageDirectory / 'fonts' + } +] + +{ #category : #accessing } +DiyaFontManager >> families [ + ^ families +] + +{ #category : #accessing } +DiyaFontManager >> familyNames [ + ^ families keys +] + +{ #category : #initialization } +DiyaFontManager >> initialize [ + super initialize. + families := Dictionary new. +] + +{ #category : #initialization } +DiyaFontManager >> loadFace: face [ + |family| + family := families at: face familyName ifAbsentPut:[DiyaFontFamily fromFace: face]. + family addFace: face +] + +{ #category : #initialization } +DiyaFontManager >> loadFile: aFile [ + | path face i numfaces | + path := aFile fullName convertToWithConverter: UTF8TextConverter new. + i := 0. + numfaces := nil. + [ face := FreeTypeFace basicNew + filename: path; + index: i. + face newFaceFromFile: path index: i. + face loadFields. + numfaces ifNil: [ numfaces := face numFaces ]. + self loadFace: face. + i := i + 1. + i < numfaces ] whileTrue: [ ] +] + +{ #category : #initialization } +DiyaFontManager >> loadFonts [ + self loadFonts: false. + +] + +{ #category : #initialization } +DiyaFontManager >> loadFonts: force [ + force ifTrue:[self reset]. + self class searchPaths do:[:path| self loadPath: path]. + +] + +{ #category : #initialization } +DiyaFontManager >> loadPath: path [ + path entries do:[:entry| entry isFile ifTrue:[self loadFile: entry ]]. + +] + +{ #category : #initialization } +DiyaFontManager >> reset [ + "reset all stored faces" + families := Dictionary new. +]