Skip to content
Merged
Show file tree
Hide file tree
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
92 changes: 51 additions & 41 deletions src/figdraw/common/fontglyphs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -72,54 +72,64 @@ proc hash*(
let variant = clampGlyphVariantSubpixelStep(subpixelVariant)
result = hash((2344, glyph.fontId, glyph.rune, lcdFiltering, variant))

proc generateGlyph*(glyph: GlyphPosition, lcdFiltering = false, subpixelVariant = 0) =
proc generateGlyph*(
glyph: GlyphPosition,
lcdFiltering = false,
subpixelVariant = 0,
force = false,
upload = true,
): Image {.discardable.} =
if unicode.isWhiteSpace(glyph.rune):
return
return nil

let
variant = clampGlyphVariantSubpixelStep(subpixelVariant)
hashFill = glyph.hash(lcdFiltering = lcdFiltering, subpixelVariant = variant)

if not hasImage(hashFill.ImageId):
let
fontId = glyph.fontId
font = getPixieFont(fontId)

var
text = $glyph.rune
arrangement = pixie.typeset(
@[newSpan(text, font)],
bounds = glyph.rect.wh.scaled(),
hAlign = CenterAlign,
vAlign = TopAlign,
wrap = false,
)
if variant > 0:
let subpixelOffset = variant.float32 / glyphVariantSubpixelSteps.float32
for i in 0 ..< arrangement.positions.len:
arrangement.positions[i].x += subpixelOffset

let snappedBounds = arrangement.computeBounds().snapToPixels()

let
lh = font.defaultLineHeight()
bounds = rect(0, 0, scaled(snappedBounds.w + snappedBounds.x), scaled(lh))

if bounds.w == 0 or bounds.h == 0:
error "GEN IMG: ", rune = $glyph.rune, wh = repr wh, snapped = repr snappedBounds
return

try:
font.paint = parseHex"FFFFFF"
var image = newImage(bounds.w.int, bounds.h.int)
image.fillText(arrangement)
if lcdFiltering:
image.applyLcdFilter()

# put into cache
if (not force) and hasImage(hashFill.ImageId):
return nil

let
fontId = glyph.fontId
font = getPixieFont(fontId)

var
text = $glyph.rune
arrangement = pixie.typeset(
@[newSpan(text, font)],
bounds = glyph.rect.wh.scaled(),
hAlign = CenterAlign,
vAlign = TopAlign,
wrap = false,
)
if variant > 0:
let subpixelOffset = variant.float32 / glyphVariantSubpixelSteps.float32
for i in 0 ..< arrangement.positions.len:
arrangement.positions[i].x += subpixelOffset

let snappedBounds = arrangement.computeBounds().snapToPixels()

let
lh = font.defaultLineHeight()
bounds = rect(0, 0, scaled(snappedBounds.w + snappedBounds.x), scaled(lh))

if bounds.w == 0 or bounds.h == 0:
debug "GEN IMG: ", rune = $glyph.rune, wh = repr wh, snapped = repr snappedBounds
return nil

try:
font.paint = parseHex"FFFFFF"
var image = newImage(bounds.w.int, bounds.h.int)
image.fillText(arrangement)
if lcdFiltering:
image.applyLcdFilter()

# put into cache
if upload:
loadImage(hashFill.ImageId, image)
except PixieError:
discard
return image
except PixieError:
return nil

iterator glyphs*(arrangement: GlyphArrangement): GlyphPosition =
var idx = 0
Expand Down
2 changes: 1 addition & 1 deletion src/figdraw/common/imgutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,4 @@ proc loadImage*(filePath: string): ImageId =

proc loadImage*(id: ImageId, image: Image) =
var imgObj = ImgObj(id: id, kind: PixieImg, pimg: image)
sendImageCached(imgObj)
sendImage(imgObj)
16 changes: 10 additions & 6 deletions src/figdraw/figrender.nim
Original file line number Diff line number Diff line change
Expand Up @@ -343,13 +343,17 @@ proc renderText(ctx: BackendContext, node: Fig) {.forbids: [AppMainThreadEff].}

ctx.setTextSubpixelShift(subpixelShift)
if glyphId notin ctx.entries:
glyph.generateGlyph(
lcdFiltering = lcdFiltering, subpixelVariant = subpixelVariant
let img = glyph.generateGlyph(lcdFiltering = lcdFiltering,
subpixelVariant = subpixelVariant,
force = true,
upload = false,
)
warn "missing glyph image in context",
glyphId = glyphId, glyphRune = $glyph.rune, glyphRuneRepr = repr(glyph.rune)
ctx.setTextSubpixelShift(0.0'f32)
continue
ctx.putImage(glyphId, img)
if glyphId in ctx.entries:
debug "missing glyph image in context",
glyphId = glyphId, glyphRune = $glyph.rune, glyphRuneRepr = repr(glyph.rune)
ctx.setTextSubpixelShift(0.0'f32)
continue

var drawPos = glyphPos
if invertText:
Expand Down
68 changes: 61 additions & 7 deletions src/figdraw/windowing/siwinshim.nim
Original file line number Diff line number Diff line change
Expand Up @@ -99,28 +99,72 @@ proc siwinWindowTitle*[BackendState](
"figdraw: " & backend & " + " & suffix

proc newSiwinWindow*(
size: IVec2, fullscreen = false, title = "FigDraw", vsync = true, msaa = 0'i32
size: IVec2,
fullscreen = false,
title = "FigDraw",
vsync = true,
msaa = 0'i32,
resizable = true,
frameless = false,
transparent = false,
): Window =
let forceOpenGl = runtimeForceOpenGlRequested()
let window =
when UseMetalBackend and not NeedSiwinOpenGLContext:
when defined(macosx):
newMetalWindowCocoa(size = size, title = title)
newMetalWindowCocoa(
size = size,
title = title,
resizable = resizable,
frameless = frameless,
transparent = transparent,
)
else:
{.error: "siwinshim: Metal backend requires macOS".}
else:
when defined(macosx):
newOpenglWindowCocoa(size = size, title = title, vsync = vsync, msaa = msaa)
newOpenglWindowCocoa(
size = size,
title = title,
vsync = vsync,
msaa = msaa,
resizable = resizable,
frameless = frameless,
transparent = transparent,
)
else:
let globals = sharedSiwinGlobals()
when UseVulkanBackend:
if forceOpenGl:
newOpenglWindow(globals, size = size, title = title, vsync = vsync)
newOpenglWindow(
globals,
size = size,
title = title,
vsync = vsync,
resizable = resizable,
frameless = frameless,
transparent = transparent,
)
else:
# Use a non-GL window for Vulkan so siwin's GL swap path does not flicker.
newSoftwareRenderingWindow(globals, size = size, title = title)
newSoftwareRenderingWindow(
globals,
size = size,
title = title,
resizable = resizable,
frameless = frameless,
transparent = transparent,
)
else:
newOpenglWindow(globals, size = size, title = title, vsync = vsync)
newOpenglWindow(
globals,
size = size,
title = title,
vsync = vsync,
resizable = resizable,
frameless = frameless,
transparent = transparent,
)
when NeedSiwinOpenGLContext:
when UseVulkanBackend:
if forceOpenGl:
Expand All @@ -140,6 +184,9 @@ proc newSiwinWindow*(
title = "FigDraw",
vsync = true,
msaa = 0'i32,
resizable = true,
frameless = false,
transparent = false,
): Window =
## Compatibility overload. Prefer creating a window first, then renderer.
let forceOpenGl = runtimeForceOpenGlRequested() or renderer.forceOpenGlByEnv()
Expand Down Expand Up @@ -173,7 +220,14 @@ proc newSiwinWindow*(

discard renderer
result = newSiwinWindow(
size = size, fullscreen = fullscreen, title = title, vsync = vsync, msaa = msaa
size = size,
fullscreen = fullscreen,
title = title,
vsync = vsync,
msaa = msaa,
resizable = resizable,
frameless = frameless,
transparent = transparent,
)

proc backingSize*(window: Window): IVec2 =
Expand Down