2022-03-04 20:28:38 +01:00
|
|
|
Class {
|
|
|
|
#name : #OpenGLFontTex,
|
|
|
|
#superclass : #OpenGLTexImage2D,
|
|
|
|
#instVars : [
|
2022-03-06 00:58:28 +01:00
|
|
|
'charmap',
|
|
|
|
'cellw',
|
|
|
|
'cellh'
|
2022-03-04 20:28:38 +01:00
|
|
|
],
|
|
|
|
#pools : [
|
|
|
|
'OpenGLConstants',
|
|
|
|
'OpenGLTypes'
|
|
|
|
],
|
|
|
|
#category : #'Diya-Fonts'
|
|
|
|
}
|
|
|
|
|
|
|
|
{ #category : #'instance creation' }
|
2022-03-06 00:58:28 +01:00
|
|
|
OpenGLFontTex class >> fromFace: face ofSize: size [
|
|
|
|
^self new fromFace: face ofSize: size; yourself
|
2022-03-04 20:28:38 +01:00
|
|
|
]
|
|
|
|
|
2022-03-06 00:58:28 +01:00
|
|
|
{ #category : #'instance creation' }
|
|
|
|
OpenGLFontTex >> blitPixel8: bitmap at: offset size: size [
|
|
|
|
size = (0@0) ifTrue:[^self].
|
|
|
|
0 to: size y - 1 do: [ :i|
|
|
|
|
LibC memCopy: (bitmap getHandle + (i* (size x))) to:(data getHandle + ((i + offset y) * width + (offset x) )) size: size x
|
|
|
|
]
|
2022-03-04 20:28:38 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
{ #category : #accessing }
|
2022-03-06 00:58:28 +01:00
|
|
|
OpenGLFontTex >> cellh [
|
|
|
|
^ cellh
|
2022-03-04 20:28:38 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
{ #category : #accessing }
|
2022-03-06 00:58:28 +01:00
|
|
|
OpenGLFontTex >> cellw [
|
|
|
|
^ cellw
|
2022-03-04 20:28:38 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
{ #category : #accessing }
|
2022-03-06 00:58:28 +01:00
|
|
|
OpenGLFontTex >> charmap [
|
|
|
|
^ charmap
|
|
|
|
]
|
|
|
|
|
|
|
|
{ #category : #'instance creation' }
|
|
|
|
OpenGLFontTex >> createBitmapFontFrom: bitmaps metrics: metrics maxBearing: maxbearing [
|
|
|
|
|currChar offset|
|
|
|
|
data ifNotNil: [data free ].
|
|
|
|
charmap := OrderedCollection new.
|
|
|
|
data := FFIExternalArray externalNewType: GLubyte size:cellw * cellh * 256.
|
|
|
|
LibC memset: data getHandle value: 0 size: data size.
|
2022-03-04 20:28:38 +01:00
|
|
|
data autoRelease.
|
2022-03-06 00:58:28 +01:00
|
|
|
currChar := 1.
|
|
|
|
offset := 0@0.
|
|
|
|
width := cellw * 16.
|
|
|
|
height := cellh * 16.
|
|
|
|
0 to: 15 do: [ :row|
|
2022-03-06 12:07:20 +01:00
|
|
|
0 to: 15 do: [ :col| |glyph|
|
2022-03-06 00:58:28 +01:00
|
|
|
offset := (col * cellw) @ (row*cellh).
|
2022-03-06 12:07:20 +01:00
|
|
|
glyph := (DiyaFontGlyph origin: offset extent: (((metrics at: currChar) first x) @ cellh)).
|
|
|
|
glyph
|
|
|
|
bearing: ((metrics at: currChar) at: 2);
|
|
|
|
advance: ((metrics at: currChar) at: 3);
|
|
|
|
texcoord: (Rectangle origin: (glyph origin/ (self extent) ) asFloatPoint corner: ((glyph corner) / (self extent)) asFloatPoint ).
|
|
|
|
charmap add:glyph.
|
2022-03-06 00:58:28 +01:00
|
|
|
self blitPixel8: (bitmaps at: currChar) at: (offset x) @ ((offset y) + maxbearing - ((metrics at: currChar) last) ) size: (metrics at: currChar) first.
|
|
|
|
currChar := currChar + 1.
|
|
|
|
]
|
|
|
|
].
|
|
|
|
]
|
|
|
|
|
|
|
|
{ #category : #'instance creation' }
|
|
|
|
OpenGLFontTex >> fromFace: face ofSize: size [
|
|
|
|
|minhang maxbearing rec metrics bitmaps |
|
|
|
|
face setPixelWidth:0 height: size.
|
|
|
|
"set up a buffer for 256 characters"
|
|
|
|
bitmaps := OrderedCollection new.
|
|
|
|
metrics := OrderedCollection new.
|
|
|
|
cellw := 0.
|
|
|
|
cellh := 0.
|
|
|
|
minhang := 0.
|
|
|
|
maxbearing := 0.
|
|
|
|
rec := (FTFaceRec fromHandle: face getHandle).
|
|
|
|
0 to: 255 do: [ :c |
|
|
|
|
|bmp bmpsize|
|
|
|
|
face loadCharacter: c flags: (1 << 2).
|
2022-03-06 12:07:20 +01:00
|
|
|
metrics add: { ((rec glyph metrics width) /64)@ ((rec glyph metrics height) /64). (face glyph hBearing). (face glyph advance). (rec glyph metrics horiBearingY) / 64}.
|
2022-03-06 00:58:28 +01:00
|
|
|
maxbearing := maxbearing max: ((rec glyph metrics horiBearingY) / 64).
|
|
|
|
cellw := cellw max: ((rec glyph metrics width) / 64).
|
|
|
|
minhang := minhang min: ((( rec glyph metrics horiBearingY) - (rec glyph metrics height )) / 64).
|
|
|
|
"copy buffer"
|
|
|
|
bmpsize := (rec glyph bitmap width)*(rec glyph bitmap rows).
|
|
|
|
bmp := FFIExternalArray externalNewType: GLubyte size:bmpsize.
|
|
|
|
LibC memCopy: rec glyph bitmap buffer to: bmp getHandle size: bmpsize.
|
|
|
|
bitmaps add: bmp.
|
|
|
|
].
|
|
|
|
cellh := maxbearing - minhang.
|
|
|
|
self createBitmapFontFrom: bitmaps metrics: metrics maxBearing: maxbearing.
|
|
|
|
bitmaps do:[:p| p free].
|
|
|
|
]
|
|
|
|
|
2022-03-06 12:07:20 +01:00
|
|
|
{ #category : #accessing }
|
|
|
|
OpenGLFontTex >> getGlyph: c [
|
|
|
|
^(self charmap at: c + 1)
|
|
|
|
]
|
|
|
|
|
2022-03-06 00:58:28 +01:00
|
|
|
{ #category : #initialization }
|
|
|
|
OpenGLFontTex >> initialize [
|
|
|
|
super initialize.
|
|
|
|
charmap := OrderedCollection new.
|
|
|
|
data := nil.
|
2022-03-04 20:28:38 +01:00
|
|
|
level := 0.
|
|
|
|
border := 0.
|
|
|
|
format := GL_ALPHA.
|
2022-03-06 00:58:28 +01:00
|
|
|
internalFormat := GL_ALPHA.
|
2022-03-04 20:28:38 +01:00
|
|
|
type := GL_UNSIGNED_BYTE.
|
|
|
|
target := GL_TEXTURE_2D.
|
|
|
|
]
|
|
|
|
|
|
|
|
{ #category : #accessing }
|
|
|
|
OpenGLFontTex >> linespace [
|
2022-03-06 00:58:28 +01:00
|
|
|
^ cellh
|
|
|
|
]
|
|
|
|
|
|
|
|
{ #category : #accessing }
|
|
|
|
OpenGLFontTex >> spacing [
|
|
|
|
^ cellw / 2
|
2022-03-04 20:28:38 +01:00
|
|
|
]
|