This commit is contained in:
Ehmry - 2023-09-11 19:00:05 +02:00
parent b28583c1ff
commit 9cc7ebab3c
6 changed files with 118 additions and 264 deletions

View File

@ -1,77 +0,0 @@
# SPDX-FileCopyrightText: ☭ Emery Hemingway
# SPDX-License-Identifier: Unlicense
import std/[deques]
import pixie
type
Pane* = ref object
image: pixie.Image
spans: seq[Span]
arrangement: Arrangement
Well* = ref object
panes: Deque[Pane]
proc newFrame(width, height: int): Frame =
let (w, h) = (width div 2, height div 2)
new result
result.panes[0] = Pane(image: newImage(w, h))
result.panes[1] = Pane(image: newImage(w, h))
result.panes[2] = Pane(image: newImage(w, h))
proc paneVec2(frame: Frame): Vec2 =
vec2(frame.panes[0].image.width.float32, frame.panes[0].image.height.float32)
proc allocChild(frame: Frame) =
assert frame.child.isNil
let (w, h) = (frame.panes[0].image.width div 2, frame.panes[0].image.height div 2)
doAssert w > 2 and h > 2, $w & "x" & $h
frame.child = newFrame(w, h)
proc setDepth(frame: Frame; n: Natural) =
var
frame = frame
i = n
while i > 0:
dec(i)
if frame.child.isNil:
allocChild(frame)
frame = frame.child
proc `[]`*(frame: Frame; i: Natural): Pane =
var
frame = frame
i = i
while true:
if i < 4:
return frame.panes[i]
if frame.child.isNil:
allocChild(frame)
frame = frame.child
dec(i, 4)
iterator walkPanes(frame: Frame): tuple[coord: Vec2, pane: Pane] =
var frame = frame
while not frame.isNil and
frame.panes[0].image.width > 2 and
frame.panes[1].image.height > 2:
let coord = frame.paneVec2
yield(coord, frame.panes[0])
yield(coord - vec2(coord.x, 0), frame.panes[1])
yield(coord - vec2(0, coord.y), frame.panes[2])
frame = frame.child
proc append*(frame: Fram; text: string; font: Font) =
let span = newSpan(text, font)
proc append*(frame: Fram; stream: Stream; font: Font) =
var line: string
while readLine(stream, line):
append(frame, font, line)
when isMainModule:
let frame = newFrame(800, 600)
frame.setDepth(3)
for (coord, pane) in walkPanes(frame):
echo coord, " - ", pane.image.width, "x", pane.image.height

View File

@ -1,26 +0,0 @@
import pixie
let image = newImage(200, 200)
image.fill(rgba(255, 255, 255, 255))
const typefacePath =
"/nix/store/ay5vhxszmibk0nrhx1vid4nhgvgdniq8-corefonts-1/share/fonts/truetype/Trebuchet_MS.ttf"
let typeface = readTypeface(typefacePath)
proc newFont(typeface: Typeface, size: float32, color: Color): Font =
result = newFont(typeface)
result.size = size
result.paint.color = color
let spans = @[
newSpan("verb [with object] ",
newFont(typeface, 12, color(0.78125, 0.78125, 0.78125, 1))),
newSpan("strallow\n", newFont(typeface, 36, color(0, 0, 0, 1))),
newSpan("\nstral·low\n", newFont(typeface, 13, color(0, 0.5, 0.953125, 1))),
newSpan("\n1. free (something) from restrictive restrictions \"the regulations are intended to strallow changes in public policy\" ",
newFont(typeface, 14, color(0.3125, 0.3125, 0.3125, 1)))
]
image.fillText(typeset(spans, vec2(180, 180)), translate(vec2(10, 10)))
image.writeFile("text_spans.png")

View File

@ -1,62 +0,0 @@
# SPDX-FileCopyrightText: ☭ Emery Hemingway
# SPDX-License-Identifier: Unlicense
import std/[sequtils]
import pixie
proc newFont*(typeface: Typeface, size: float32): Font =
result = newFont(typeface)
result.size = size
func height(arr: Arrangement): float =
if arr.positions.len > 0: result = arr.positions[arr.positions.high].y
proc add(result: var Arrangement; other: sink Arrangement) =
if result.lines.len == 0:
result = other
else:
let runeOff = result.runes.len
add(result.lines, map(other.lines,
proc (x: (int, int)): (int, int) = (x[0]+runeOff, x[1]+runeOff)))
add(result.spans, map(other.spans,
proc (x: (int, int)): (int, int) = (x[0]+runeOff, x[1]+runeOff)))
add(result.fonts, other.fonts)
add(result.runes, other.runes)
let yOff = result.positions[result.positions.high].y
add(result.positions,
map(other.positions,
proc(pos: Vec2): Vec2 = vec2(pos.x, pos.y + yOff)))
add(result.selectionRects,
map(other.selectionRects,
proc(rect: Rect): Rect = rect(rect.x, rect.y + yOff, rect.w, rect.h)))
proc render*(font: Font; text: string): Image =
# TODO: render by font size, not by wh
var wh = layoutBounds(font, "X")
echo "bounds of X are ", wh
wh.x = wh.x * 80
wh.y = wh.y * 50
let margin = wh / 9.0
var
printSpace = wh * (7.0 / 9.0)
pages = @[Arrangement()]
proc pageEnd(): float =
let
pi = pages.high
li = pages[pi].lines.high
ci = pages[pi].lines[li][1]
pages[pi].positions[ci].y
proc append(span: Span) =
var arr = typeset(@[span], printSpace)
if pages[pages.high].height + arr.height < printSpace.y:
pages[pages.high].add arr
else:
discard
append(newSpan(text, font))
result = newImage(int wh.x, int wh.y)
fill(result, rgba(255, 255, 255, 255))
fillText(result, pages[0], translate(margin))

View File

@ -46,7 +46,6 @@ type
screen: Image
window: WindowPtr
renderer: RendererPtr
texture: TexturePtr
rect: bumpy.Rect
viewPoint: Vec2
well: Well
@ -56,59 +55,50 @@ func rect(img: Image): bumpy.Rect =
result.w = float img.width
result.h = float img.height
proc newApp(length: cint): App =
proc newApp(): App =
## Create a new square plane of `length` pixels.
result = App(
well: newWell(length, length),
zoomFactor: 1.0,
result = App(zoomFactor: 1.0)
result.window = createWindow(
"well of text",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
600, 800,
SDL_WINDOW_RESIZABLE,
)
discard createWindowAndRenderer(
length, length,
SDL_WINDOW_RESIZABLE,
result.window, result.renderer)
var info: RendererInfo
result.renderer = createRenderer(result.window, -1, 0)
var
info: RendererInfo
# mode: DisplayMode
check getRendererInfo(result.renderer, addr info)
echo "SDL Renderer: ", info.name
echo "SDL maximum texture size: ", info.max_texture_width, "x", info.max_texture_height
#check getDisplayMode(result.window, mode)
let (w, h) = result.window.getSize
result.well = newWell(w, h)
func toSdl(rect: bumpy.Rect): sdl2.Rect =
(result.x, result.y, result.w, result.h) =
(cint rect.x, cint rect.y, cint rect.w, cint rect.h)
proc viewPort(app: App; wh: Vec2): bumpy.Rect =
result.wh = wh / app.zoomFactor
result.xy = app.viewPoint - (result.wh * 0.5)
proc redraw(app: App) =
assert app.zoomFactor != 0.0
app.renderer.setDrawColor(0xf8, 0xf8, 0xf8)
app.renderer.clear()
var
(w, h) = app.window.getSize
sdlViewPort = rect(-float(w shr 1), -float(h shr 1), float w, float h)
viewPort = app.viewPort(sdlViewPort.wh)
app.renderer.setDrawColor(0x80, 0x80, 0x80)
app.renderer.clear()
wh = vec2(float w, float h)
viewPort = rect(app.viewPoint, wh / app.zoomFactor)
for intersect in app.well.intersectingPanes(viewPort):
let texture = app.well.texture(intersect.index, app.renderer)
if texture.isNil:
break
var
sdlSrc = intersect.src.toSdl
dst = rect(intersect.dst.xy - viewPort.xy, intersect.dst.wh)
if app.zoomFactor != 1.0:
dst = dst * app.zoomFactor
var sdlDst = dst.toSdl
app.renderer.copy(texture, addr sdlSrc, addr sdlDst)
let wellRect = app.well.rect
if viewPort.overlaps wellRect:
for (index, rect) in app.well.intersectingPanes(viewPort):
let texture = app.well.texture(index, app.renderer, rect.wh * app.zoomFactor)
if texture.isNil: break
let
overlap = viewPort and rect
src = rect(overlap.xy - rect.xy, overlap.wh)
var dst: bumpy.Rect
dst.x = (overlap.x - viewPort.x) * (sdlViewPort.w / viewPort.w)
dst.y = (overlap.y - viewPort.y) * (sdlViewPort.h / viewPort.h)
dst.wh =
if app.zoomFactor == 1.0:
overlap.wh # correct
elif app.zoomFactor > 1.0:
sdlViewPort.wh - dst.xy # correct?
else:
overlap.wh * app.zoomFactor
var (sdlSrc, sdlDst) = (src.toSdl, dst.toSdl)
app.renderer.copy(texture, addr sdlSrc, addr sdlDst)
else:
echo "no overlap of ", viewPort, " and ", wellRect
app.renderer.present()
proc resize(app: App) =
@ -124,21 +114,22 @@ proc pan(app: App; xy: Vec2) =
app.redraw()
proc recenter(app: App) =
reset app.viewPoint
let (w, h) = app.window.getSize
app.viewPoint = vec2(0, 0)
app.zoomFactor = 1.0
app.redraw()
proc main() =
discard sdl2.init(INIT_TIMER or INIT_VIDEO or INIT_EVENTS)
let app = newApp(512)
let app = newApp()
app.redraw()
let
typeface = readTypeface(typefacePath)
font = newFont(typeface)
# TODO
app.redraw()
#[
let
@ -157,16 +148,16 @@ proc main() =
let stream = newFileStream(stdin)
# TODO
var line: string
while readLine(stream, line):
# TODO
app.well.append(line, font)
app.redraw()
var
evt = sdl2.defaultEvent
mousePanning: bool
line: string
while true:
if readLine(stream, line):
# TODO
app.well.append(line, font)
app.redraw()
# asyncdispatch.poll(0)
if waitEventTimeout(evt, sdlTimeout):
case evt.kind

View File

@ -1 +0,0 @@
debugger:native

View File

@ -8,38 +8,48 @@ type
Rect = bumpy.Rect
Pane* = ref object
image: pixie.Image
spans: seq[Span]
arrangement: Arrangement
image: Image
texture: TexturePtr
Well* = ref object
panes: Deque[Pane]
dimensions: Vec2
width, height: int
using
pane: Pane
well: Well
proc newWell*(width, height: int): Well =
result = Well(dimensions: vec2(float width, float height))
result = Well(width: width, height: height)
result.panes.addFirst Pane()
proc margins*(well): Vec2 = well.dimensions / 9.0
proc dimensions(well): Vec2 = vec2(float well.width, float well.height)
proc bounds*(well): Vec2 = well.margins * 7.0
proc margins*(well): (int, int) = (well.width div 9, well.height div 9)
proc rect*(well): Rect =
result.wh = well.dimensions
proc rectAt(well; offset: int): Rect =
if offset < well.panes.len:
let n = succ offset
result.w = well.dimensions.x / float(2 * n)
result.h = well.dimensions.y / float(2 * n)
case offset and 3
of 0: (result.x, result.y) = (result.w, result.h)
of 1: result.y = result.h
of 2: result.x = result.w
proc rectAt(well; i: int): Rect =
if i < well.panes.len:
let
(quo, rem) = divmod(i, 3)
shiftOff = quo.succ
scaleDiv = 1 shl quo.succ
fullWh = well.dimensions
result.w = float(well.width shr shiftOff)
result.h = float(well.height shr shiftOff)
case rem
of 0:
result.xy = fullWh - (result.wh * 2.0)
of 1:
result.x = fullWh.x - result.w
result.y = fullWh.y - (result.h * 2.0)
of 2:
result.x = fullWh.x - (result.w * 2.0)
result.y = fullWh.y - result.h
else: discard
proc place*(well; offset, width, height: int): (Pane, Rect) =
@ -52,20 +62,20 @@ proc place*(well; offset, width, height: int): (Pane, Rect) =
proc append*(well; text: string; font: Font) =
assert well.panes.len > 0
let span = newSpan(text, font)
let span = newSpan(text & "\n", font)
while true:
let pane = well.panes[0]
let pane = well.panes.peekLast()
pane.spans.add(span)
var
arrangement = typeset(pane.spans, well.bounds)
arrangement = typeset(pane.spans, well.dimensions)
bounds = layoutBounds arrangement
# assert bounds.x <= well.bounds.x
if bounds.y <= well.bounds.y:
doAssert pane.spans.len > 0, "text does not find on a single pane - " & $bounds & $well.bounds
if bounds.y <= well.dimensions.y:
doAssert pane.spans.len > 0, "text does not find on a single pane - " & $bounds & $well.dimensions
pane.arrangement = arrangement
break
else:
well.panes.addFirst Pane()
discard pane.spans.pop()
well.panes.addLast Pane()
proc append*(well; stream: Stream; font: Font) =
var line: string
@ -79,21 +89,12 @@ proc render*(well; index: Natural; scale: float): Image =
if dimensions.x > 8 and dimensions.y > 8:
result = newImage(int dimensions.x, int dimensions.y)
fill(result, rgba(255, 255, 255, 255))
fillText(well.panes[index].arrangment, translate(well.margin))
fillText(well.panes[index].arrangment, translate(well.margins))
]#
iterator intersectingPanes*(well; rect: Rect): (int, Rect) =
var i = 0
while i < well.panes.len:
let bounds = well.rectAt(i)
if (i and 3) == 3:
let quad = rect(vec2(0,0), bounds.wh * 2.0)
if not rect.overlaps quad:
# all further panes are non-intersecting
break
if rect.overlaps bounds:
yield (i, bounds)
inc(i)
type Intersection = tuple
src, dst: Rect
index: int
const
amask = uint32 0xff000000
@ -101,20 +102,48 @@ const
gmask = uint32 0x0000ff00
bmask = uint32 0x00ff0000
proc texture*(well; index: int; renderer: RendererPtr; wh: Vec2): TexturePtr =
# TODO:­caching
if index >= well.panes.len or wh.x < 4 or wh.y < 4: return nil
let pane = well.panes[index]
let
image = newImage(wh.x.int, wh.y.int)
zoom = wh / well.dimensions
margin = wh / 9.0
if not pane.arrangement.isNil:
image.fillText(pane.arrangement, scale(zoom) * translate(margin))
var surface = createRGBSurfaceFrom(
image.data[0].addr, wh.x.cint, wh.y.cint, cint 32, 4 * wh.x.cint,
rmask, gmask, bmask, amask,
)
result = createTextureFromSurface(renderer, surface)
destroy(surface)
proc texture*(well; index: int; renderer: RendererPtr): TexturePtr =
if index < well.panes.len:
let pane = well.panes[index]
if pane.texture.isNil and not pane.arrangement.isNil:
assert pane.image.isNil
let
w = (well.width div 9) * 7
h = (well.height div 9) * 7
pane.image = newImage(w, h)
pane.image.fill(rgba(255, 255, 255, 255))
pane.image.fillText(pane.arrangement)
var surface = createRGBSurfaceFrom(
pane.image.data[0].addr,
pane.image.width.cint,
pane.image.height.cint,
cint 32,
pane.image.width.cint shl 2,
rmask, gmask, bmask, amask,
)
assert(not surface.isNil, $getError())
pane.texture = createTextureFromSurface(renderer, surface)
destroy(surface)
assert(not pane.texture.isNil, $getError())
result = pane.texture
iterator intersectingPanes*(well; view: Rect): Intersection =
var
intersect: Intersection
zoom = 1.0
while intersect.index < well.panes.len:
var paneRect = well.rectAt(intersect.index)
if (intersect.index mod 3) == 0:
let quad = rect(paneRect.xy, paneRect.wh * 2.0)
if not view.overlaps quad:
# all further panes are non-intersecting
break
zoom = zoom * 2.0
let margin = paneRect.wh / 9.0
paneRect.xy = paneRect.xy + margin
paneRect.wh = paneRect.wh - (margin * 2.0)
if view.overlaps paneRect:
intersect.dst = view and paneRect
intersect.src = rect(intersect.dst.xy - paneRect.xy, intersect.dst.wh) * zoom
yield(intersect)
inc(intersect.index)