Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement symmetric text padding from edges #46

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions stl/geometry/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type textRenderConfig struct {
contextWidth int
contextHeight int
fontSize float64
alignment gg.Align
}

// ImageConfig holds parameters for image rendering
Expand All @@ -36,9 +37,8 @@ type imageRenderConfig struct {
}

const (
imagePosition = 0.025
usernameOffset = -0.01
yearPosition = 0.77
imagePosition = 0.025
textPadding = 0.1 // 10%

defaultContextWidth = 800
defaultContextHeight = 200
Expand Down Expand Up @@ -69,7 +69,7 @@ func Create3DText(username string, year string, innerWidth, baseHeight float64)

usernameConfig := textRenderConfig{
renderConfig: renderConfig{
startX: innerWidth * usernameOffset,
startX: innerWidth * textPadding,
startY: -textDepthOffset / 2,
startZ: baseHeight * usernameZOffset,
voxelScale: textVoxelSize,
Expand All @@ -79,11 +79,12 @@ func Create3DText(username string, year string, innerWidth, baseHeight float64)
contextWidth: usernameContextWidth,
contextHeight: usernameContextHeight,
fontSize: usernameFontSize,
alignment: gg.AlignLeft,
}

yearConfig := textRenderConfig{
renderConfig: renderConfig{
startX: innerWidth * yearPosition,
startX: innerWidth*(1-textPadding) - 75,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By chosing 75, It exaclty aligns with right side of the cuboid.

The number was chosen by hit-and-trial method.

startY: -textDepthOffset / 2,
startZ: baseHeight * yearZOffset,
voxelScale: textVoxelSize * 0.75,
Expand All @@ -93,6 +94,7 @@ func Create3DText(username string, year string, innerWidth, baseHeight float64)
contextWidth: yearContextWidth,
contextHeight: yearContextHeight,
fontSize: yearFontSize,
alignment: gg.AlignRight,
}

usernameTriangles, err := renderText(usernameConfig)
Expand Down Expand Up @@ -129,7 +131,7 @@ func renderText(config textRenderConfig) ([]types.Triangle, error) {
dc.SetRGB(0, 0, 0)
dc.Clear()
dc.SetRGB(1, 1, 1)
dc.DrawStringAnchored(config.text, float64(config.contextWidth)/8, float64(config.contextHeight)/2, 0.0, 0.5)
dc.DrawStringWrapped(config.text, 0, float64(config.contextHeight)/2, 0.0, 0.5, float64(config.contextWidth), 1.5, config.alignment)

var triangles []types.Triangle

Expand Down