[orx-text-writer] Add horizontal and vertical text alignment features
Introduced horizontalAlign and verticalAlign properties in WriteStyle and TextWriter, enabling precise text alignment within bounding boxes. Enhanced text token management to support alignment calculations and adjusted demos to showcase the new capabilities.
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
import org.openrndr.application
|
||||
import org.openrndr.color.ColorRGBa
|
||||
import org.openrndr.draw.TextSettingMode
|
||||
import org.openrndr.draw.isolated
|
||||
import org.openrndr.draw.loadFont
|
||||
import org.openrndr.extra.textwriter.writer
|
||||
import org.openrndr.shape.Rectangle
|
||||
import kotlin.math.cos
|
||||
|
||||
/**
|
||||
* This demo features the drawing of a centered rectangle and the addition of styled text inside
|
||||
@@ -24,9 +26,7 @@ import org.openrndr.shape.Rectangle
|
||||
* - `writer` facilitates text rendering with alignment and spacing adjustments.
|
||||
*/
|
||||
fun main() {
|
||||
|
||||
application {
|
||||
|
||||
program {
|
||||
extend {
|
||||
val r = Rectangle.fromCenter(drawer.bounds.center, 200.0, 200.0)
|
||||
@@ -37,9 +37,15 @@ fun main() {
|
||||
}
|
||||
drawer.fontMap = loadFont("demo-data/fonts/IBMPlexMono-Regular.ttf", 24.0)
|
||||
writer {
|
||||
drawer.drawStyle.textSetting = TextSettingMode.SUBPIXEL
|
||||
style.horizontalAlign = cos(seconds) * 0.5 + 0.5
|
||||
box = r.offsetEdges(-10.0)
|
||||
newLine()
|
||||
text("hello world")
|
||||
newLine()
|
||||
text("this is a test")
|
||||
newLine()
|
||||
text("centered")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
29
orx-text-writer/src/jvmDemo/kotlin/DemoTextWriter02.kt
Normal file
29
orx-text-writer/src/jvmDemo/kotlin/DemoTextWriter02.kt
Normal file
@@ -0,0 +1,29 @@
|
||||
import org.openrndr.application
|
||||
import org.openrndr.draw.loadFont
|
||||
import org.openrndr.extra.shapes.primitives.grid
|
||||
import org.openrndr.extra.textwriter.writer
|
||||
import kotlin.math.PI
|
||||
import kotlin.math.cos
|
||||
|
||||
fun main() {
|
||||
application {
|
||||
configure {
|
||||
width = 720
|
||||
height = 720
|
||||
}
|
||||
program {
|
||||
extend {
|
||||
val columns = drawer.bounds.grid(3, 3).flatten()
|
||||
drawer.fontMap = loadFont("demo-data/fonts/IBMPlexMono-Regular.ttf", 24.0)
|
||||
for ((index, column) in columns.withIndex()) {
|
||||
writer {
|
||||
style.horizontalAlign = cos(seconds + (index/8.0) * 2 * PI) * 0.5 + 0.5
|
||||
box = column.offsetEdges(-20.0)
|
||||
newLine()
|
||||
text("DEAR FRIENDS.\nIT TOOK A WHILE, BUT NOW WE HAVE HORIZONTAL TEXT ALIGNMENT!\nLET'S CELEBRATE")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
35
orx-text-writer/src/jvmDemo/kotlin/DemoTextWriter03.kt
Normal file
35
orx-text-writer/src/jvmDemo/kotlin/DemoTextWriter03.kt
Normal file
@@ -0,0 +1,35 @@
|
||||
import org.openrndr.application
|
||||
import org.openrndr.color.ColorRGBa
|
||||
import org.openrndr.draw.isolated
|
||||
import org.openrndr.draw.loadFont
|
||||
import org.openrndr.extra.shapes.primitives.grid
|
||||
import org.openrndr.extra.textwriter.writer
|
||||
|
||||
fun main() {
|
||||
application {
|
||||
configure {
|
||||
width = 720
|
||||
height = 720
|
||||
}
|
||||
program {
|
||||
extend {
|
||||
val columns = drawer.bounds.grid(3, 3).flatten()
|
||||
drawer.fontMap = loadFont("demo-data/fonts/IBMPlexMono-Regular.ttf", 16.0)
|
||||
for ((index, column) in columns.withIndex()) {
|
||||
drawer.isolated {
|
||||
drawer.stroke = ColorRGBa.WHITE
|
||||
drawer.fill = null
|
||||
drawer.rectangle(column.offsetEdges(-10.0))
|
||||
}
|
||||
|
||||
writer {
|
||||
style.verticalAlign = (index/3) / 2.0
|
||||
style.horizontalAlign = index.mod(3) / 2.0
|
||||
box = column.offsetEdges(-20.0)
|
||||
text(listOf("DEAR FRIENDS.","IT TOOK A WHILE, BUT NOW WE HAVE TEXT ALIGNMENT IN BOTH DIRECTIONS!", "LET'S CELEBRATE"))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user