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

257 lines
5.3 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 : [
2022-03-04 20:28:38 +01:00
'data',
2022-03-19 02:18:29 +01:00
'wrap',
2022-03-23 00:52:15 +01:00
'texheight',
'font',
'lines',
'maxLineWidth'
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
]
{ #category : #'menu messages' }
DiyaText >> align [
2022-08-07 20:43:16 +02:00
^ self ? #xAlign
]
{ #category : #accessing }
DiyaText >> alignLine:w [
self align = #center ifTrue:[^ 0 max:((self extent x - w) / 2.0 ) asFloat].
self align = #right ifTrue:[^ 0 max: self extent x - w].
^0
]
{ #category : #initialization }
DiyaText >> allocMemory [
vbuffer ifNotNil: [
vbuffer size >= data size
ifTrue: [^self]
ifFalse:[vbuffer free]
].
vbuffer := FFIExternalArray externalNewType: GLfloat size: data size * 16.
vbuffer autoRelease.
]
2022-08-07 20:43:16 +02:00
{ #category : #accessing }
DiyaText >> borderWidth [
^0
]
2022-03-01 22:58:58 +01:00
{ #category : #accessing }
DiyaText >> data [
^ data
]
{ #category : #accessing }
DiyaText >> data: anObject [
data := anObject.
2022-08-07 20:43:16 +02:00
self setDirty
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 [
self splitLines.
self formatText .
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-08-07 20:43:16 +02:00
self setDirty
2022-03-16 01:32:01 +01:00
]
2022-03-03 19:19:40 +01:00
{ #category : #accessing }
DiyaText >> fontName [
2022-08-07 20:43:16 +02:00
^ self ? #fontFamily
2022-03-03 19:19:40 +01:00
]
2022-03-01 22:58:58 +01:00
{ #category : #accessing }
DiyaText >> fontSize [
2022-08-07 20:43:16 +02:00
^ self ? #fontSize
2022-03-01 22:58:58 +01:00
]
{ #category : #accessing }
DiyaText >> fontStyle [
2022-08-07 20:43:16 +02:00
^ self ? #fontStyle
2022-03-01 22:58:58 +01:00
]
2022-03-06 12:07:20 +01:00
{ #category : #accessing }
DiyaText >> formatText [
|offset index line|
lines ifNil: [^self].
offset := 0@(self valignText: (texture linespace) * (lines size)).
index := 1.
lines do:[:assoc|
line := assoc value.
offset setX: (self alignLine: assoc key) setY: offset y.
line do:[:g|
(self getCharsVerticesFrom: g offset: offset cellh: texture cellh) do:[:v|
vbuffer at: index put:v.
index := index + 1.
]
].
offset setX: 0.0 setY: (offset y ) + (texture linespace)
].
]
{ #category : #accessing }
DiyaText >> getCharsVerticesFrom:glyph offset: offset cellh: cellh [
|x y w h gsize texcoord|
gsize := glyph extent.
2022-03-06 18:33:10 +01:00
x := offset x + (glyph bearing x).
y := offset y "- cellh".
2022-03-06 18:33:10 +01:00
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. texcoord origin x. texcoord origin y.
x. y + h. texcoord origin x. texcoord corner y.
x + w. y + h. 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. 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 >> getLineAt: index to: line with: tex2D [
| glyph ret w |
w := 0.
index to: data size do:[ :i|
glyph := tex2D getGlyph: (data at:i) asInteger.
(w + (glyph advance x)) >= (self extent x) ifTrue:[
wrap ifFalse: [ ^ i @ w].
ret := self lastSeparatorFrom: i.
ret to: i - 1 do:[:j|
w := w - (line removeLast advance x)].
^ (ret+1)@w
] ifFalse:[
w := w + (glyph advance x).
line add: glyph.
].
].
maxLineWidth := maxLineWidth max: w.
^ (data size + 1) @ w
]
{ #category : #initialization }
DiyaText >> initTexture [
font:= DiyaFontManager uniqueInstance style: self fontStyle from: self fontName.
texture := font textureOf: self fontSize.
2022-03-21 01:45:21 +01:00
]
2022-03-01 22:58:58 +01:00
{ #category : #initialization }
DiyaText >> initialize [
super initialize.
data := nil.
wrap := false.
2022-03-19 02:18:29 +01:00
texheight := 0.
type := GL_QUADS.
maxLineWidth := 0.
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 >> lines [
^ lines
]
{ #category : #accessing }
DiyaText >> maxLineHeight [
texture ifNil: [ ^0].
^ texture linespace
]
{ #category : #accessing }
DiyaText >> maxLineWidth [
^ maxLineWidth
]
{ #category : #accessing }
DiyaText >> splitLines [
|line ret tex2D|
ret := 1@0.
tex2D := self texture.
lines := OrderedCollection new.
[
line := OrderedCollection new.
ret := self getLineAt: ret x to: line with: tex2D.
lines add: ((ret y) -> line).
(ret x < data size) and: wrap
] whileTrue.
]
2022-03-06 12:07:20 +01:00
{ #category : #accessing }
DiyaText >> texture [
texture ifNil: [ self initTexture ].
texheight = texture height ifFalse: [
texheight := texture height.
2022-03-19 02:18:29 +01:00
self update.
2022-08-07 20:43:16 +02:00
self setClean.
2022-03-19 02:18:29 +01:00
].
^texture
2022-03-06 12:07:20 +01:00
]
{ #category : #initialization }
DiyaText >> update [
bbox ifNil: [ ^true ].
data ifNil:[^true].
data ifEmpty:[^true].
vbuffer ifNotNil: [vbuffer free].
vbuffer := FFIExternalArray externalNewType: GLfloat size: data size * 16.
vbuffer autoRelease.
self drawText.
^true
]
{ #category : #'menu messages' }
DiyaText >> valign [
2022-08-07 20:43:16 +02:00
^ self ? #yAlign
]
{ #category : #accessing }
DiyaText >> valignText:h [
self valign = #middle ifTrue:[^ (0 max:((self extent y - h) / 2 ) asFloat)].
self valign = #bottom ifTrue:[^ (0 max:(self extent y - h ))].
^0
]
{ #category : #initialization }
DiyaText >> wordWrap: aBool [
wrap := aBool.
dirty := true
]