Skip to content

Commit

Permalink
Fix SVGs being rendered upside down (I'm dumb)
Browse files Browse the repository at this point in the history
  • Loading branch information
Deftu committed Oct 10, 2024
1 parent 3fa0c22 commit 40b231b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ interface NanoVgConstants {

int NVG_HOLE();

int NVG_IMAGE_FLIPY();

}

NanoVgConstants constants();
Expand Down Expand Up @@ -115,7 +117,7 @@ interface NanoVgConstants {

float textBounds(float x, float y, String text, float[] bounds);

int createImage(float width, float height, ByteBuffer buffer);
int createImage(float width, float height, ByteBuffer buffer, int flags);

void scissor(float x, float y, float w, float h);

Expand All @@ -139,6 +141,6 @@ interface NanoVgConstants {

void deleteSvg(long address);

void rasterizeSvg(long address, float x, float y, int w, int h, float scale, int stride, ByteBuffer data);
void rasterizeSvg(long address, float x, float y, float scale, ByteBuffer data, int w, int h, int stride);

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ class NanoVgImpl(
return NanoVG.NVG_HOLE
}

override fun NVG_IMAGE_FLIPY(): Int {
return NanoVG.NVG_IMAGE_FLIPY
}

}

private var handle: Long = -1
Expand Down Expand Up @@ -223,17 +227,14 @@ class NanoVgImpl(
}

override fun fontSize(size: Float) {
// println("> [$handle] Setting font size: $size")
NanoVG.nvgFontSize(handle, size)
}

override fun fontFaceId(id: Int) {
// println("> [$handle] Setting font face ID: $id")
NanoVG.nvgFontFaceId(handle, id)
}

override fun textAlign(align: Int) {
// println("> [$handle] Setting text align: $align")
NanoVG.nvgTextAlign(handle, align)
}

Expand All @@ -242,12 +243,11 @@ class NanoVgImpl(
}

override fun textBounds(x: Float, y: Float, text: String, bounds: FloatArray): Float {
// println("> [$handle] Getting text bounds for: $text")
return NanoVG.nvgTextBounds(handle, x, y, text, bounds)
}

override fun createImage(width: Float, height: Float, buffer: ByteBuffer): Int {
return NanoVG.nvgCreateImageRGBA(handle, width.toInt(), height.toInt(), NanoVG.NVG_IMAGE_FLIPY, buffer)
override fun createImage(width: Float, height: Float, buffer: ByteBuffer, flags: Int): Int {
return NanoVG.nvgCreateImageRGBA(handle, width.toInt(), height.toInt(), flags, buffer)
}

override fun scissor(x: Float, y: Float, w: Float, h: Float) {
Expand Down Expand Up @@ -307,28 +307,24 @@ class NanoVgImpl(
}

override fun parseSvg(data: ByteBuffer): Triple<Long, Float, Float> {
println("> [$handle - $svgHandle] Parsing SVG data")
val result = NanoSVG.nsvgParse(data, PIXELS, 96f) ?: throw IllegalStateException("Failed to parse SVG data")
println("> [$handle - $svgHandle] Parsed SVG data: ${result.address()} - ${result.width()}x${result.height()}")
return Triple(result.address(), result.width(), result.height())
}

override fun deleteSvg(address: Long) {
println("> [$handle - $svgHandle] Deleting SVG data: $address")
NanoSVG.nsvgDelete(NSVGImage.create(address))
}

override fun rasterizeSvg(
address: Long,
x: Float,
y: Float,
scale: Float,
data: ByteBuffer,
w: Int,
h: Int,
scale: Float,
stride: Int,
data: ByteBuffer
stride: Int
) {
println("> [$handle - $svgHandle] Rasterizing SVG data: $x, $y, $w, $h, $scale, $stride")
NanoSVG.nsvgRasterize(svgHandle, NSVGImage.create(address), x, y, scale, data, w, h, stride)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class RendererImpl(
if (defaultImage == 0) {
val iImage = PolyUI.defaultImage
val iData = iImage.load()
val iHandle = nanoVg.createImage(iImage.width, iImage.height, iData.toDirectByteBuffer())
val iHandle = nanoVg.createImage(iImage.width, iImage.height, iData.toDirectByteBuffer(), 0)
require(iHandle != 0) { "NanoVG failed to initialize default image" }
defaultImageData = iData
images[iImage] = iHandle
Expand Down Expand Up @@ -398,7 +398,7 @@ class RendererImpl(
val heightOutput = IntArray(1)
val result = stb.loadFromMemory(buffer, widthOutput, heightOutput, IntArray(1), 4)
image.size = Vec2(widthOutput[0].toFloat(), heightOutput[0].toFloat())
nanoVg.createImage(image.width, image.height, result)
nanoVg.createImage(image.width, image.height, result, 0)
}
}
return defaultImage
Expand Down Expand Up @@ -458,7 +458,7 @@ class RendererImpl(
result
}

nanoVg.createImage(image.width, image.height, buffer)
nanoVg.createImage(image.width, image.height, buffer, 0)
}

PolyImage.Type.Vector -> {
Expand Down Expand Up @@ -505,14 +505,10 @@ class RendererImpl(
val h = (height * 2f).toInt()

val dest = lwjgl.memAlloc(w * h * 4)
val scale = (if (abs((width / svgWidth) - 1f) <= abs((height / svgHeight) - 1f)) {
width / svgWidth
} else {
height / svgHeight
}) * 2f
val scale = cl1(width / svgWidth, height / svgHeight) * 2f

nanoVg.rasterizeSvg(address, 0f, 0f, w, h, scale, w * 4, dest)
return nanoVg.createImage(w.toFloat(), h.toFloat(), dest)
nanoVg.rasterizeSvg(address, 0f, 0f, scale, dest, w, h, w * 4)
return nanoVg.createImage(w.toFloat(), h.toFloat(), dest, 0)
}

private fun populateNvgColor(argb: Int, colorAddress: Long) {
Expand Down

0 comments on commit 40b231b

Please sign in to comment.