changeset 1335:f2ff6f624932

fix: image copies of font atlas
author sam <sam@basx.dev>
date Thu, 22 Aug 2024 22:31:05 +0700
parents d5a6f69dc855
children 5ca89802a746
files semicongine/image.nim semicongine/text/font.nim tests/test_text.nim
diffstat 3 files changed, 17 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/semicongine/image.nim	Thu Aug 22 22:18:33 2024 +0700
+++ b/semicongine/image.nim	Thu Aug 22 22:31:05 2024 +0700
@@ -49,6 +49,11 @@
 
 proc `=copy`[S, T](dest: var ImageObject[S, T], source: ImageObject[S, T]) {.error.}
 
+func copy*[S, T](img: ImageObject[S, T]): ImageObject[S, T] =
+  for bf, rf in fields(img, result):
+    rf = bf
+
+
 # loads single layer image
 proc loadImageData*[T: PixelType](
     pngData: string | seq[uint8]
--- a/semicongine/text/font.nim	Thu Aug 22 22:18:33 2024 +0700
+++ b/semicongine/text/font.nim	Thu Aug 22 22:31:05 2024 +0700
@@ -72,7 +72,6 @@
   var
     topOffsets: Table[Rune, int]
     images: seq[Image[Gray]]
-  let empty_image = Image[Gray](width: 1, height: 1, data: @[[0'u8]])
 
   for codePoint in codePoints:
     var
@@ -101,7 +100,7 @@
         bitmap[i] = [data[i].uint8]
       images.add Image[Gray](width: width.uint32, height: height.uint32, data: bitmap)
     else:
-      images.add empty_image
+      images.add Image[Gray](width: 1, height: 1, data: @[[0'u8]])
 
     nativeFree(data)
 
@@ -109,15 +108,14 @@
 
   result.fontAtlas = packed.atlas
 
-  let w = float32(packed.atlas.width)
-  let h = float32(packed.atlas.height)
+  let w = float32(result.fontAtlas.width)
+  let h = float32(result.fontAtlas.height)
   for i in 0 ..< codePoints.len:
     let
       codePoint = codePoints[i]
-      image = images[i]
       coord = (x: float32(packed.coords[i].x), y: float32(packed.coords[i].y))
-      iw = float32(image.width)
-      ih = float32(image.height)
+      iw = float32(images[i].width)
+      ih = float32(images[i].height)
     # horizontal spaces:
     var advance, leftBearing: cint
     stbtt_GetCodepointHMetrics(
@@ -125,7 +123,7 @@
     )
 
     result.glyphs[codePoint] = GlyphInfo(
-      dimension: vec2(float32(image.width), float32(image.height)),
+      dimension: vec2(float32(images[i].width), float32(images[i].height)),
       uvs: [
         vec2((coord.x + 0.5) / w, (coord.y + ih - 0.5) / h),
         vec2((coord.x + 0.5) / w, (coord.y + 0.5) / h),
--- a/tests/test_text.nim	Thu Aug 22 22:18:33 2024 +0700
+++ b/tests/test_text.nim	Thu Aug 22 22:31:05 2024 +0700
@@ -18,7 +18,7 @@
   var pipeline =
     createPipeline[DefaultFontShader[FontDS]](renderPass = vulkan.swapchain.renderPass)
 
-  var ds = asDescriptorSetData(FontDS(fontAtlas: font.fontAtlas))
+  var ds = asDescriptorSetData(FontDS(fontAtlas: font.fontAtlas.copy()))
   uploadImages(renderdata, ds)
   initDescriptorSet(renderdata, pipeline.layout(0), ds)
 
@@ -55,15 +55,15 @@
   var pipeline =
     createPipeline[DefaultFontShader[FontDS]](renderPass = vulkan.swapchain.renderPass)
 
-  var ds1 = asDescriptorSetData(FontDS(fontAtlas: font1.fontAtlas))
+  var ds1 = asDescriptorSetData(FontDS(fontAtlas: font1.fontAtlas.copy()))
   uploadImages(renderdata, ds1)
   initDescriptorSet(renderdata, pipeline.layout(0), ds1)
 
-  var ds2 = asDescriptorSetData(FontDS(fontAtlas: font2.fontAtlas))
+  var ds2 = asDescriptorSetData(FontDS(fontAtlas: font2.fontAtlas.copy()))
   uploadImages(renderdata, ds2)
   initDescriptorSet(renderdata, pipeline.layout(0), ds2)
 
-  var ds3 = asDescriptorSetData(FontDS(fontAtlas: font3.fontAtlas))
+  var ds3 = asDescriptorSetData(FontDS(fontAtlas: font3.fontAtlas.copy()))
   uploadImages(renderdata, ds3)
   initDescriptorSet(renderdata, pipeline.layout(0), ds3)
 
@@ -128,7 +128,7 @@
   var pipeline =
     createPipeline[DefaultFontShader[FontDS]](renderPass = vulkan.swapchain.renderPass)
 
-  var ds = asDescriptorSetData(FontDS(fontAtlas: font.fontAtlas))
+  var ds = asDescriptorSetData(FontDS(fontAtlas: font.fontAtlas.copy()))
   uploadImages(renderdata, ds)
   initDescriptorSet(renderdata, pipeline.layout(0), ds)
 
@@ -201,7 +201,7 @@
   var pipeline =
     createPipeline[DefaultFontShader[FontDS]](renderPass = vulkan.swapchain.renderPass)
 
-  var ds = asDescriptorSetData(FontDS(fontAtlas: font.fontAtlas))
+  var ds = asDescriptorSetData(FontDS(fontAtlas: font.fontAtlas.copy()))
   uploadImages(renderdata, ds)
   initDescriptorSet(renderdata, pipeline.layout(0), ds)