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

154 lines
3.6 KiB
Smalltalk
Raw Normal View History

2022-03-01 22:58:58 +01:00
Class {
#name : #DiyaText,
#superclass : #Diya2DNode,
#instVars : [
'fontStyle',
'fontSize',
2022-03-03 19:19:40 +01:00
'fontName',
2022-03-04 20:28:38 +01:00
'data',
'style'
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
]
2022-03-03 19:19:40 +01:00
{ #category : #accessing }
DiyaText >> draw [
data ifNil: [ ^self ].
2022-03-06 12:07:20 +01:00
self shader
2022-03-06 18:33:10 +01:00
setUniform: #u_use_texture value:1.
2022-03-03 19:19:40 +01:00
"configure vao vbo for texture QUAD"
2022-03-06 18:33:10 +01:00
style := DiyaFontManager uniqueInstance style: self fontStyle from: self fontName.
self texture setup.
2022-03-06 12:07:20 +01:00
context texture0 setImage2D: self texture.
2022-03-06 18:33:10 +01:00
context texture0 active.
2022-03-03 19:19:40 +01:00
context vao enableAttribute: 0.
2022-03-04 20:28:38 +01:00
OpenGLVertexArray vertexAttributePointerIndex: 0 size:4 type: GL_FLOAT normalized: GL_FALSE stride: 16 pointer: nil .
2022-03-06 12:07:20 +01:00
self drawText.
2022-03-03 19:19:40 +01:00
context vao disableAttribute: 0.
"reset value"
2022-03-06 18:33:10 +01:00
self texture drop.
2022-03-03 19:19:40 +01:00
]
2022-03-06 12:07:20 +01:00
{ #category : #accessing }
DiyaText >> drawSubStringFrom: lower at: offset [
2022-03-06 18:33:10 +01:00
|upper vertices count index tex2D|
upper := lower + ((data size - lower) min:(context buffer size / 24) asInteger).
2022-03-06 12:07:20 +01:00
count := 0.
2022-03-06 18:33:10 +01:00
index := 1.
tex2D := self texture.
2022-03-06 12:07:20 +01:00
lower to: upper do: [ :i|
2022-03-06 18:33:10 +01:00
vertices := self getCharsVertices:(data at:i) asciiValue offset: offset on: tex2D.
vertices do: [
:e| context buffer at: index put:e.
index := index + 1.
].
vertices ifNotEmpty: [ count := count + 6 ].
2022-03-06 12:07:20 +01:00
].
context vbo subData: GL_ARRAY_BUFFER offset: 0 data: context buffer.
OpenGL drawArrays: GL_TRIANGLES first:0 count:count.
^ upper + 1
]
{ #category : #accessing }
DiyaText >> drawText [
|lower offset|
offset := 0@0.
lower := 1.
[
lower := self drawSubStringFrom: lower at:offset.
lower < data size.
] whileTrue.
2022-03-03 19:19:40 +01:00
]
{ #category : #accessing }
DiyaText >> fontName [
^ fontName
]
{ #category : #accessing }
DiyaText >> fontName: anObject [
2022-03-04 20:28:38 +01:00
fontName := anObject.
2022-03-03 19:19:40 +01:00
]
2022-03-01 22:58:58 +01:00
{ #category : #accessing }
DiyaText >> fontSize [
^ fontSize
]
{ #category : #accessing }
DiyaText >> fontSize: anObject [
2022-03-04 20:28:38 +01:00
fontSize := anObject.
2022-03-01 22:58:58 +01:00
]
{ #category : #accessing }
DiyaText >> fontStyle [
^ fontStyle
]
{ #category : #accessing }
DiyaText >> fontStyle: anObject [
2022-03-04 20:28:38 +01:00
fontStyle := anObject.
2022-03-01 22:58:58 +01:00
]
2022-03-06 12:07:20 +01:00
{ #category : #accessing }
2022-03-06 18:33:10 +01:00
DiyaText >> getCharsVertices:c offset: offset on: tex2D [
|x y w h glyph gsize|
2022-03-06 12:07:20 +01:00
c = (Character space asciiValue) ifTrue:[
2022-03-06 18:33:10 +01:00
offset setX: (offset x + (tex2D spacing ) ) setY: offset y.
^ {}.
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:[
offset setX: 0.0 setY: (offset y )- (tex2D linespace).].
x := offset x + (glyph bearing x).
y := offset y - (tex2D cellh).
w := (gsize x).
h := (gsize y).
offset setX: (offset x + (glyph advance x)) setY: offset y.
2022-03-06 12:07:20 +01:00
^{x. y + h. glyph texcoord origin x. glyph texcoord origin y.
x. y. glyph texcoord origin x. glyph texcoord corner y.
x + w. y. glyph texcoord corner x. glyph texcoord corner y.
x. y + h. glyph texcoord origin x. glyph texcoord origin y.
x + w. y. glyph texcoord corner x. glyph texcoord corner y.
x + w. y + h. glyph texcoord corner x. glyph texcoord origin y. }.
]
2022-03-01 22:58:58 +01:00
{ #category : #initialization }
DiyaText >> initialize [
super initialize.
2022-03-04 20:28:38 +01:00
self fontName: 'Ubuntu'.
self fontStyle: 'Regular'.
2022-03-06 18:33:10 +01:00
self fontSize: 16.
2022-03-01 22:58:58 +01:00
data := nil.
]
2022-03-06 12:07:20 +01:00
{ #category : #accessing }
DiyaText >> texture [
^style textureOf: self fontSize
]