1
0
mirror of https://github.com/lxsang/Diya-API.git synced 2024-12-28 12:28:21 +01:00
Diya-API/Diya/DiyaText.class.st

193 lines
4.2 KiB
Smalltalk
Raw Normal View History

2022-03-01 22:58:58 +01:00
Class {
#name : #DiyaText,
2022-03-19 02:18:29 +01:00
#superclass : #Diya2DPrimShape,
2022-03-01 22:58:58 +01:00
#instVars : [
'fontStyle',
'fontSize',
2022-03-03 19:19:40 +01:00
'fontName',
2022-03-04 20:28:38 +01:00
'data',
'style',
2022-03-19 02:18:29 +01:00
'wrap',
'texheight'
2022-03-01 22:58:58 +01:00
],
2022-03-03 19:19:40 +01:00
#pools : [
'FT2Types'
],
2022-03-01 22:58:58 +01:00
#category : #'Diya-Graphics'
}
2022-03-03 19:19:40 +01:00
{ #category : #'as yet unclassified' }
DiyaText class >> data: string [
^ (self new) data: string; yourself
]
{ #category : #'as yet unclassified' }
DiyaText class >> data: string shader: s [
^ (self with:s) data: string; yourself
]
2022-03-01 22:58:58 +01:00
{ #category : #accessing }
DiyaText >> data [
^ data
]
{ #category : #accessing }
DiyaText >> data: anObject [
data := anObject.
dirty := true
2022-03-01 22:58:58 +01:00
]
2022-03-19 02:18:29 +01:00
{ #category : #initialization }
DiyaText >> drawBorder [
2022-03-21 01:45:21 +01:00
self shouldNotBeCalled
]
{ #category : #initialization }
DiyaText >> drawLines [
^ self shouldNotBeCalled
2022-03-03 19:19:40 +01:00
]
2022-03-06 12:07:20 +01:00
{ #category : #accessing }
DiyaText >> drawText [
2022-03-21 01:45:21 +01:00
|vertices index tex2D offset lbdelta|
2022-03-06 18:33:10 +01:00
index := 1.
offset := 0@0.
2022-03-06 18:33:10 +01:00
tex2D := self texture.
2022-03-21 01:45:21 +01:00
lbdelta := self getLinebreakIndices: (self extent x / tex2D meanww) asInteger.
1 to: data size do: [ :i|
2022-03-16 01:32:01 +01:00
vertices := self getCharsVerticesAt:i offset: offset on: tex2D.
2022-03-06 18:33:10 +01:00
vertices do: [
:e| vbuffer at: index put:e.
2022-03-06 18:33:10 +01:00
index := index + 1.
].
2022-03-16 01:32:01 +01:00
(offset x > self extent x and: wrap not) ifTrue: [ ^self ].
2022-03-21 01:45:21 +01:00
((lbdelta includes: i + 1) and: wrap) ifTrue: [
offset setX: 0.0 setY: (offset y )- (tex2D linespace).
(offset y negated > self extent y) ifTrue: [ ^self ].
].
2022-03-06 12:07:20 +01:00
].
2022-03-03 19:19:40 +01:00
]
2022-03-16 01:32:01 +01:00
{ #category : #accessing }
DiyaText >> extent: v [
2022-03-16 21:33:35 +01:00
bbox := Rectangle origin: 0@0 corner: (v x) @ (v y negated ).
2022-03-16 01:32:01 +01:00
dirty := true
]
2022-03-03 19:19:40 +01:00
{ #category : #accessing }
DiyaText >> fontName [
^ fontName
]
{ #category : #initialization }
DiyaText >> fontName: name style: face size: size [
2022-03-21 01:45:21 +01:00
name ifNotNil: [fontName := name].
face ifNotNil: [fontStyle := face].
fontName := fontName ifNil: [DiyaFontManager uniqueInstance defaultFamily].
fontStyle := fontStyle ifNil: [DiyaFontManager uniqueInstance defaultStyle].
fontSize := size.
style := DiyaFontManager uniqueInstance style: self fontStyle from: self fontName.
dirty := true.
2022-03-03 19:19:40 +01:00
]
2022-03-01 22:58:58 +01:00
{ #category : #accessing }
DiyaText >> fontSize [
^ fontSize
]
{ #category : #initialization }
DiyaText >> fontSize: size [
self fontName: nil style:nil size: size
2022-03-01 22:58:58 +01:00
]
{ #category : #accessing }
DiyaText >> fontStyle [
^ fontStyle
]
2022-03-06 12:07:20 +01:00
{ #category : #accessing }
2022-03-16 01:32:01 +01:00
DiyaText >> getCharsVerticesAt:i offset: offset on: tex2D [
|x y w h glyph gsize c texcoord|
2022-03-21 01:45:21 +01:00
c := (data at:i) asInteger.
2022-03-06 12:07:20 +01:00
glyph := tex2D getGlyph: c.
2022-03-06 18:33:10 +01:00
gsize := glyph extent.
((offset x > self extent x) and: (gsize x > 0)) ifTrue:[
wrap ifFalse: [ ^ { } ].
].
2022-03-06 18:33:10 +01:00
x := offset x + (glyph bearing x).
y := offset y - (tex2D cellh).
w := (gsize x).
h := (gsize y).
texcoord := glyph texcoord.
2022-03-06 18:33:10 +01:00
offset setX: (offset x + (glyph advance x)) setY: offset y.
^{x. y + h. texcoord origin x. texcoord origin y.
x. y. texcoord origin x. texcoord corner y.
x + w. y. texcoord corner x. texcoord corner y.
x. y + h. texcoord origin x. texcoord origin y.
x + w. y. texcoord corner x. texcoord corner y.
x + w. y + h. texcoord corner x. texcoord origin y. }.
2022-03-06 12:07:20 +01:00
]
2022-03-21 01:45:21 +01:00
{ #category : #accessing }
DiyaText >> getLinebreakIndices: delta [
|indices idx|
indices := Set new.
idx := delta + 1.
[
idx < data size
] whileTrue: [
indices add: (self lastSeparatorFrom: idx) + 1.
idx := idx + delta.
].
^indices
]
2022-03-01 22:58:58 +01:00
{ #category : #initialization }
DiyaText >> initialize [
super initialize.
2022-03-21 01:45:21 +01:00
"self fontName: 'Ubuntu' style:'Regular' size: 16."
2022-03-01 22:58:58 +01:00
data := nil.
wrap := false.
2022-03-19 02:18:29 +01:00
bbox := nil.
texheight := 0.
2022-03-21 01:45:21 +01:00
self fontSize: 16
2022-03-16 01:32:01 +01:00
]
{ #category : #'as yet unclassified' }
2022-03-21 01:45:21 +01:00
DiyaText >> lastSeparatorFrom: index [
index to: 1 by: -1 do: [:i|
(data at: i) isSeparator ifTrue:[^i].
2022-03-16 01:32:01 +01:00
].
^ 0
2022-03-01 22:58:58 +01:00
]
2022-03-06 12:07:20 +01:00
{ #category : #accessing }
DiyaText >> texture [
2022-03-19 02:18:29 +01:00
|tex|
tex := style textureOf: self fontSize.
texheight = tex height ifFalse: [
texheight := tex height.
self update.
dirty := false.
].
^tex
2022-03-06 12:07:20 +01:00
]
{ #category : #initialization }
DiyaText >> update [
2022-03-16 01:32:01 +01:00
bbox ifNil: [ ^false ].
vbuffer ifNotNil: [vbuffer free].
vbuffer := FFIExternalArray externalNewType: GLfloat size: data size * 24.
vbuffer autoRelease.
self drawText.
^true
]
{ #category : #initialization }
DiyaText >> wordWrap: aBool [
wrap := aBool.
dirty := true
]