Files

802 lines
34 KiB
Ziran

// A small, host-independent RGBA renderer and original stroke lettering.
using Tk :: #import "tk/widgets"
using UTF :: #import "tk/text"
using View :: #import "tk/view"
using Scale :: #import "tk/scale"
using Check :: #import "tk/check"
using Scroll :: #import "tk/scrollbar"
using Items :: #import "tk/canvas"
#if !#defined(TK_NO_CANVAS) {
using Math :: #import "std/math"
}
#if #defined(TK_MENU) {
using Menu :: #import "tk/menu"
}
#if #defined(TK_BITMAP) {
using Bitmap :: #import "tk/bitmap"
using Images :: #import "tk/image"
#if !#defined(TK_NO_CANVAS) {
using Fonts :: #import "tk/font"
}
}
#import "std/vec"
Canvas :: struct {
pixels: []u8;
width: s32;
height: s32;
first: s32;
#if !#defined(TK_NO_CANVAS) {
clipped: bool;
clip: Tk.Box;
}
}
Ink :: struct { r: u8; g: u8; b: u8; }
PAPER :: Ink.{r = 244, g = 246, b = 247};
BUTTON :: Ink.{r = 224, g = 234, b = 242};
PRESSED :: Ink.{r = 188, g = 213, b = 231};
BORDER :: Ink.{r = 119, g = 149, b = 173};
LETTER :: Ink.{r = 25, g = 47, b = 67};
MUTED :: Ink.{r = 112, g = 123, b = 132};
ENTRY :: Ink.{r = 255, g = 255, b = 255};
SELECTION :: Ink.{r = 255, g = 228, b = 145};
LINK :: Ink.{r = 30, g = 98, b = 145};
Fill :: (canvas: *Canvas, box: Tk.Box, ink: Ink) {
left := Tk.NonNegative(box.x);
top := Tk.Maximum(box.y, canvas.first);
right := Tk.Minimum(box.x + box.w, canvas.width);
bottom := Tk.Minimum(box.y + box.h, canvas.height);
#if !#defined(TK_NO_CANVAS) {
if canvas.clipped {
clip := canvas.clip;
left = Tk.Maximum(left, clip.x);
top = Tk.Maximum(top, clip.y);
right = Tk.Minimum(right, clip.x+clip.w);
bottom = Tk.Minimum(bottom, clip.y+clip.h);
}
}
for y: top..bottom-1 {
offset := cast(s64)(y-canvas.first) * cast(s64)canvas.width * 4;
for x: left..right-1 {
i := offset + cast(s64)x * 4;
canvas.pixels[i] = ink.r;
canvas.pixels[i+1] = ink.g;
canvas.pixels[i+2] = ink.b;
canvas.pixels[i+3] = 255;
}
}
}
// Each group is x0,y0,x1,y1 on a 5 by 7 grid. Lowercase uses the same glyphs.
Strokes :: (rune: s32) -> string {
byte := rune;
if byte >= 97 && byte <= 122 { byte -= 32 }
if byte == #char " " { return "" }
if byte == #char "A" { return "0610203046400343" }
if byte == #char "B" { return "00060030304141423033423333444545360636" }
if byte == #char "C" { return "404030100001010510364646" }
if byte == #char "D" { return "000600303041414545360636" }
if byte == #char "E" { return "0006004003330646" }
if byte == 233 || byte == 201 { return "00060141043406463021" }
if byte == #char "F" { return "000600400333" }
if byte == #char "G" { return "4030100001050536364646432343" }
if byte == #char "H" { return "000640460343" }
if byte == #char "I" { return "004020260646" }
if byte == #char "J" { return "4045453606360604" }
if byte == #char "K" { return "000640030346" }
if byte == #char "L" { return "00060646" }
if byte == #char "M" { return "0600002323404046" }
if byte == #char "N" { return "060000464640" }
if byte == #char "O" { return "10301001010505161636364545414130" }
if byte == #char "P" { return "000600303041414242330333" }
if byte == #char "Q" { return "103010010105051616363645454141302446" }
if byte == #char "R" { return "0006003030414142423303332346" }
if byte == #char "S" { return "40303010100101020213333333444445453636161605" }
if byte == #char "T" { return "00402026" }
if byte == #char "U" { return "00050516163636454540" }
if byte == #char "V" { return "00264026" }
if byte == #char "W" { return "0016162323363640" }
if byte == #char "X" { return "00464006" }
if byte == #char "Y" { return "002340232326" }
if byte == #char "Z" { return "004040060646" }
if byte == #char "0" { return "103010010105051616363645454141300541" }
if byte == #char "1" { return "022020260646" }
if byte == #char "2" { return "011010303041414242060646" }
if byte == #char "3" { return "003030414142423313333344444545360636" }
if byte == #char "4" { return "3200030343434046" }
if byte == #char "5" { return "4000000303333344444545360636" }
if byte == #char "6" { return "403010000005051616363645454444330333" }
if byte == #char "7" { return "00404016" }
if byte == #char "8" { return "103010010102021333333342424141301304040505161636364545444433" }
if byte == #char "9" { return "43431313020201011010303041414545360636" }
if byte == #char "+" { return "03432125" }
if byte == #char "-" { return "0343" }
if byte == #char "." { return "2626" }
if byte == 40 { return "30101002020404161636" }
if byte == 41 { return "10303042424444363616" }
if byte == 47 { return "0640" }
if byte == 92 { return "0046" }
if byte == 44 { return "2516" }
if byte == 58 { return "22222525" }
if byte == 94 { return "02202042" }
if byte == 59 { return "22222516" }
if byte == 39 { return "2021" }
if byte == 34 { return "10113031" }
if byte == 95 { return "0646" }
if byte == 61 { return "02420444" }
if byte == 91 { return "301010161636" }
if byte == 93 { return "103030363616" }
if byte == 60 { return "40030346" }
if byte == 62 { return "00434306" }
if byte == 42 { return "1234123414320343" }
if byte == 124 { return "2026" }
return "0110103030414142422323242626";
}
Magnitude :: (value: s32) -> s32 { return ifx value < 0 then -value else value }
Stroke :: (canvas: *Canvas, origin_x: s32, origin_y: s32, segment: string, ink: Ink) {
x := cast(s32)segment[0] - 48;
y := cast(s32)segment[1] - 48;
end_x := cast(s32)segment[2] - 48;
end_y := cast(s32)segment[3] - 48;
dx := Magnitude(end_x - x);
dy := -Magnitude(end_y - y);
sx: s32 = ifx x < end_x then 1 else -1;
sy: s32 = ifx y < end_y then 1 else -1;
error := dx + dy;
while true {
Fill(canvas, .{x = origin_x + 2*x, y = origin_y + 2*y, w = 2, h = 2}, ink);
if x == end_x && y == end_y { break }
doubled := 2 * error;
if doubled >= dy { error += dy; x += sx }
if doubled <= dx { error += dx; y += sy }
}
}
DrawText :: (canvas: *Canvas, box: Tk.Box, text: string, center: bool = true, ink: Ink = LETTER) {
if box.h < Items.TEXT_HEIGHT { return }
length := UTF.Count(text, Tk.Minimum(box.w/Tk.CHAR_WIDTH, 512));
x := ifx center then box.x + (box.w - length*Tk.CHAR_WIDTH)/2 else box.x;
y := box.y + (box.h-Items.TEXT_HEIGHT)/2;
offset: s64;
for i: 0..length-1 {
rune, size := UTF.Decode(text, offset);
offset += size;
glyph := Strokes(rune);
for segment: 0..glyph.count/4-1 {
at := segment * 4;
Stroke(canvas, x + i*Tk.CHAR_WIDTH + 1, y, glyph[at:at+4], ink);
}
}
}
TextRow :: (canvas: *Canvas, view: *View.State, box: Tk.Box, line: string, start: s64) {
end := start + line.count;
for * view.ranges {
if it.last <= start || it.first >= end { continue }
tag := *view.tags[it.tag];
selected := TextView(tag.name) == "sel";
if !selected && !tag.underline { continue }
first := ifx it.first > start then it.first-start else 0;
last := ifx it.last < end then it.last-start else line.count;
before := UTF.Count(line[:first]);
cells := UTF.Count(line[first:last]);
x := before * Tk.CHAR_WIDTH;
width := Tk.Minimum(cells * Tk.CHAR_WIDTH, box.w-x);
if selected { Fill(canvas, .{x = box.x+x, y = box.y, w = width, h = box.h-2}, SELECTION) }
if tag.underline { Fill(canvas, .{x = box.x+x, y = box.y+box.h-2, w = width, h = 1}, LINK) }
}
DrawText(canvas, box, line, false);
}
DrawEntry :: (canvas: *Canvas, widget: *Tk.Widget, active: bool) {
box := widget.box;
Fill(canvas, box, ifx active then LETTER else BORDER);
Fill(canvas, .{x = box.x+1, y = box.y+1, w = box.w-2, h = box.h-2}, ENTRY);
if box.w > 2 * Tk.ENTRY_INSET && box.h >= 18 {
start := Tk.EntryStart(widget);
text := TextView(widget.text);
inner_x := box.x + Tk.ENTRY_INSET;
DrawText(canvas, .{x = inner_x, y = box.y, w = Tk.EntrySlots(box) * Tk.CHAR_WIDTH, h = box.h}, text[start:], false);
if active {
x := inner_x + UTF.Count(text[start:widget.cursor]) * Tk.CHAR_WIDTH;
Fill(canvas, .{x = x, y = box.y+4, w = 1, h = box.h-8}, LETTER);
}
}
}
RenderFocusedEntry :: (pixels: []u8, width: s32, height: s32) {
canvas := Canvas.{pixels = pixels, width = width, height = height};
widget := *Tk.widgets[Tk.focused];
if widget.order != 0 && widget.box.w >= 4 && widget.box.h >= 4 { DrawEntry(*canvas, widget, true) }
}
// Paint the rows from first (inclusive) through height (exclusive).
Render :: (pixels: []u8, width: s32, height: s32, first: s32 = 0) {
canvas := Canvas.{pixels = pixels, width = width, height = height, first = first};
Fill(*canvas, .{w = width, h = height}, PAPER);
#if #defined(TK_BITMAP) {
DrawFrames(*canvas, -1)
}
for * Tk.widgets {
if it.order == 0 || it.kind == Tk.FRAME { continue }
box := it.box;
#if #defined(TK_BITMAP) {
if it.kind == Tk.LABEL {
if box.w > 0 && box.h > 0 { DrawLabel(*canvas, it) }
continue;
}
}
if box.w < 4 || box.h < 4 { continue }
if it.kind == Tk.SCALE { DrawScale(*canvas, it, it_index); continue }
if Tk.SelectionButton(it.kind) { DrawCheck(*canvas, it); continue }
if it.kind == Tk.SCROLLBAR { DrawScroll(*canvas, it, it_index); continue }
if it.kind == Tk.PANEL { DrawPanel(*canvas, it); continue }
#if #defined(TK_LISTBOX) {
if it.kind == Tk.LISTBOX { DrawListbox(*canvas, it, it_index == Tk.focused); continue }
}
#if !#defined(TK_NO_CANVAS) {
if it.kind == Tk.CANVAS { DrawCanvas(*canvas, it); continue }
}
if it.kind == Tk.TEXT {
Fill(*canvas, box, ifx it_index == Tk.focused then LETTER else BORDER);
Fill(*canvas, .{x = box.x+1, y = box.y+1, w = box.w-2, h = box.h-2}, ENTRY);
if box.h < 2 * View.INSET + View.ROW_HEIGHT { continue }
text := TextView(it.text);
for row: 0..it.view.visible-1 {
index := it.view.top + row;
if index >= it.view.rows.count { break }
line := View.Row(*it.view, text, index);
TextRow(*canvas, *it.view, .{x = box.x + View.INSET, y = box.y + View.INSET + row*View.ROW_HEIGHT,
w = box.w - 2*View.INSET, h = View.ROW_HEIGHT}, line, it.view.rows[index]);
}
continue;
}
if it.kind == Tk.ENTRY {
DrawEntry(*canvas, it, it_index == Tk.focused);
continue;
}
#if #defined(TK_MENU) {
if it.kind == Tk.MENUBUTTON { DrawMenuButton(*canvas, it, it_index); continue }
}
if it.kind == Tk.BUTTON {
Fill(*canvas, box, BORDER);
Fill(*canvas, .{x = box.x+1, y = box.y+1, w = box.w-2, h = box.h-2},
ifx it_index == Tk.pressed then PRESSED else BUTTON);
}
DrawText(*canvas, box, TextView(it.text), true, ifx it.disabled then MUTED else LETTER);
}
#if #defined(TK_MENU) {
if Tk.MenuPosted() { DrawMenu(*canvas) }
}
}
#if #defined(TK_BITMAP) {
DrawFrames :: (canvas: *Canvas, parent: s64) {
index := Tk.NextChild(parent, 0);
while index >= 0 {
widget := *Tk.widgets[index];
if widget.kind == Tk.FRAME && widget.box.w > 0 && widget.box.h > 0 {
DrawAppearance(canvas, widget.box, widget.style);
DrawFrames(canvas, index);
}
index = Tk.NextChild(parent, widget.order);
}
}
DrawAppearance :: (canvas: *Canvas, box: Tk.Box, style: Tk.Style) {
bg := ifx style.background == Items.NONE then PAPER else Color(style.background);
if style.background != Items.NONE { Fill(canvas, box, bg) }
if style.relief == .Flat { return }
light := Ink.{cast(u8)Tk.Minimum(cast(s32)bg.r+96, 255),
cast(u8)Tk.Minimum(cast(s32)bg.g+96, 255),
cast(u8)Tk.Minimum(cast(s32)bg.b+96, 255)};
dark := Ink.{bg.r/2, bg.g/2, bg.b/2};
if style.background == 0 { light = .{96, 96, 96}; dark = .{48, 48, 48} }
limit := Tk.Minimum(style.border, Tk.Minimum(box.w, box.h)/2);
for i: 0..limit-1 {
ridge := style.relief == .Ridge || style.relief == .Groove;
raised := style.relief == .Raised || style.relief == .Ridge;
if ridge && i >= (style.border+1)/2 { raised = !raised }
top := ifx raised then light else dark;
bottom := ifx raised then dark else light;
offset := cast(s32)i;
x := box.x+offset;
y := box.y+offset;
w := box.w-2*offset;
h := box.h-2*offset;
Fill(canvas, .{x, y, w, 1}, top);
Fill(canvas, .{x, y, 1, h}, top);
Fill(canvas, .{x, y+h-1, w, 1}, bottom);
Fill(canvas, .{x+w-1, y+1, 1, h-1}, bottom);
}
}
DrawLabel :: (canvas: *Canvas, widget: *Tk.Widget) {
style := widget.style;
box := widget.box;
DrawAppearance(canvas, box, style);
border := style.border;
box = .{x = box.x+border, y = box.y+border, w = box.w-2*border, h = box.h-2*border};
if box.w <= 0 || box.h <= 0 { return }
image := Tk.LabelImage(widget);
if image != null {
foreground := ifx style.foreground == Items.NONE then Color(0) else Color(style.foreground);
x := box.x+(box.w-image.pixels.width)/2;
y := box.y+(box.h-image.pixels.height)/2;
DrawBitmap(canvas, box, x, y, image, foreground, true);
} else {
foreground := ifx style.foreground == Items.NONE then LETTER else Color(style.foreground);
DrawText(canvas, box, TextView(widget.text), true, foreground);
}
}
bitmap_palette: [256]u32;
bitmap_palette_ready: bool;
DrawBitmap :: (canvas: *Canvas, clip: Tk.Box, x: s32, y: s32, image: *Images.Entry,
ink: Ink, stencil: bool = false) {
bitmap := *image.pixels;
mask := *image.mask;
left := Tk.Maximum(x, Tk.NonNegative(clip.x));
top := Tk.Maximum(y, Tk.Maximum(clip.y, canvas.first));
right := Tk.Minimum(x+bitmap.width, Tk.Minimum(clip.x+clip.w, canvas.width));
bottom := Tk.Minimum(y+bitmap.height, Tk.Minimum(clip.y+clip.h, canvas.height));
if left >= right || top >= bottom { return }
masked := mask.pixels.count > 0;
source := bitmap.pixels.count > 0;
mono := bitmap.encoding == .Mask;
if source && !mono && !bitmap_palette_ready {
for i: 0..255 { bitmap_palette[i] = Bitmap.Color(cast(s32)i) }
bitmap_palette_ready = true;
}
dx := cast(s64)bitmap.left-mask.left;
dy := cast(s64)bitmap.top-mask.top;
foreground := cast(u32)ink.r | (cast(u32)ink.g << 8) | (cast(u32)ink.b << 16);
for row: top..bottom-1 {
sy := cast(s64)row-y;
my := sy+dy;
if masked && (my < 0 || my >= mask.height) { continue }
for column: left..right-1 {
sx := cast(s64)column-x;
mx := sx+dx;
if masked && (mx < 0 || mx >= mask.width || !Bitmap.Bit(mask, mx, my)) { continue }
color: u32;
if source {
if !mono { color = bitmap_palette[bitmap.pixels[sy*bitmap.pitch+sx]] }
else {
set := Bitmap.Bit(bitmap, sx, sy);
if stencil && !masked {
if !set { continue }
color = foreground;
} else if set { color = 0xffffff }
}
}
destination := ((row-canvas.first)*canvas.width+column)*4;
canvas.pixels[destination] = cast(u8)color;
canvas.pixels[destination+1] = cast(u8)(color >> 8);
canvas.pixels[destination+2] = cast(u8)(color >> 16);
canvas.pixels[destination+3] = 255;
}
}
}
}
#if #defined(TK_MENU) {
DrawMenuButton :: (canvas: *Canvas, widget: *Tk.Widget, index: s64) {
box := widget.box;
Fill(canvas, box, BORDER);
Fill(canvas, .{x = box.x+1, y = box.y+1, w = box.w-2, h = box.h-2},
ifx Tk.popup_owner == index then PRESSED else BUTTON);
ink := ifx widget.disabled then MUTED else LETTER;
DrawText(canvas, .{x = box.x+2, y = box.y, w = box.w-14, h = box.h}, TextView(widget.text), true, ink);
x := box.x+box.w-10;
y := box.y+box.h/2-1;
for i: 0..2 {
offset := cast(s32)i;
Fill(canvas, .{x = x+offset, y = y+offset, w = 5-2*offset, h = 1}, ink);
}
}
DrawMenu :: (canvas: *Canvas) {
box := Tk.MenuBounds();
menu := *Tk.widgets[Tk.popup].menu;
border := menu.border;
Fill(canvas, box, BORDER);
x := box.x+border;
y := box.y+border;
width := box.w-2*border;
bottom := box.y+box.h-border;
Fill(canvas, .{x = x, y = y, w = width, h = bottom-y}, PAPER);
for * menu.rows {
height := Tk.Minimum(Menu.Height(it), bottom-y);
if height <= 0 { break }
if it.kind == .Separator {
Fill(canvas, .{x = x+4, y = y+height/2, w = width-8, h = 1}, BORDER);
} else {
indicator := Menu.IndicatorWidth(it);
selected := Menu.SelectionEntry(it) && Check.Selected(*it.check);
if menu.active == it_index || indicator == 0 && selected {
Fill(canvas, .{x = x, y = y, w = width, h = height}, PRESSED);
}
ink := ifx it.disabled then MUTED else LETTER;
if indicator > 0 && width >= 24 && height >= 16 {
DrawIndicator(canvas, x+5, y+(height-16)/2, it.check.radio, selected, ink);
}
DrawText(canvas, .{x = x+8+indicator, y = y, w = width-16-indicator, h = height},
TextView(it.label), false, ink);
}
y += height;
}
}
}
#if #defined(TK_LISTBOX) {
using Listbox :: #import "tk/listbox"
DrawListbox :: (canvas: *Canvas, widget: *Tk.Widget, focused: bool) {
box := widget.box;
list := *widget.listbox;
Fill(canvas, box, ifx focused then LETTER else BORDER);
Fill(canvas, .{x = box.x+1, y = box.y+1, w = box.w-2, h = box.h-2}, ENTRY);
for row: 0..list.visible-1 {
index := list.top+row;
if index >= list.rows.count { break }
y := box.y + Listbox.INSET + cast(s32)row * Listbox.ROW_HEIGHT;
entry := *list.rows[index];
if entry.selected {
Fill(canvas, .{x = box.x+Listbox.INSET, y = y, w = box.w-2*Listbox.INSET, h = Listbox.ROW_HEIGHT}, SELECTION);
}
DrawText(canvas, .{x = box.x+5, y = y, w = box.w-10, h = Listbox.ROW_HEIGHT}, TextView(entry.text), false);
if focused && index == list.active {
Fill(canvas, .{x = box.x+Listbox.INSET, y = y+Listbox.ROW_HEIGHT-1, w = box.w-2*Listbox.INSET, h = 1}, BORDER);
}
}
}
}
#if !#defined(TK_NO_CANVAS) || #defined(TK_BITMAP) {
Color :: (rgb: s32) -> Ink {
return .{r = cast(u8)(rgb >> 16), g = cast(u8)(rgb >> 8), b = cast(u8)rgb};
}
}
#if !#defined(TK_NO_CANVAS) {
#if #defined(TK_BITMAP) {
DrawFontText :: (canvas: *Canvas, font: *Fonts.Font, box: Tk.Box, text: string, ink: Ink) {
clip := canvas.clip;
left := Tk.NonNegative(clip.x);
top := Tk.NonNegative(clip.y);
right := Tk.Minimum(clip.x+clip.w, canvas.width);
bottom := Tk.Minimum(clip.y+clip.h, canvas.height);
if box.y >= bottom || box.y+box.h <= top { return }
line := box.y;
at: s64;
while at < text.count && text[at] != 0 && line < bottom {
end, next := Fonts.Line(text, at);
pen := box.x;
while at < end {
rune, size := UTF.Decode(text, at);
at += size;
glyph := Fonts.Character(font, rune);
if glyph.atlas == null { continue }
x := pen + glyph.left;
y := line + glyph.rise;
first_column := Tk.Maximum(0, left-x);
last_column := Tk.Minimum(glyph.width, right-x);
first_row := Tk.Maximum(glyph.top, Tk.Maximum(line, top)-y);
last_row := Tk.Minimum(glyph.bottom, Tk.Minimum(line+font.height, bottom)-y);
for row: first_row..last_row-1 {
start := (cast(s64)(y+row-canvas.first)*canvas.width + x)*4;
for column: first_column..last_column-1 {
if !Bitmap.Bit(glyph.atlas, glyph.x+column, row) { continue }
offset := start+4*column;
canvas.pixels[offset] = ink.r;
canvas.pixels[offset+1] = ink.g;
canvas.pixels[offset+2] = ink.b;
canvas.pixels[offset+3] = 255;
}
}
pen += glyph.advance;
}
line += font.height;
at = next;
}
}
}
DrawCanvas :: (canvas: *Canvas, widget: *Tk.Widget) {
box := widget.box;
border := widget.canvas.border;
if widget.canvas.sunken && border > 0 {
light := Ink.{207, 207, 207};
dark := Ink.{159, 159, 159};
Fill(canvas, box, light);
Fill(canvas, .{x = box.x, y = box.y, w = box.w, h = border}, dark);
Fill(canvas, .{x = box.x, y = box.y, w = border, h = box.h}, dark);
}
box = .{x = box.x+border, y = box.y+border, w = box.w-2*border, h = box.h-2*border};
if box.w <= 0 || box.h <= 0 { return }
if widget.canvas.background != Items.NONE { Fill(canvas, box, Color(widget.canvas.background)) }
clipped := Canvas.{pixels = canvas.pixels, width = canvas.width, height = canvas.height,
first = canvas.first, clipped = true, clip = box};
bottom := Tk.Minimum(box.y+box.h, canvas.height);
clipped.clip.y = Tk.Maximum(box.y, canvas.first);
clipped.clip.h = Tk.NonNegative(bottom-clipped.clip.y);
origin_x := box.x - widget.canvas.viewport.x;
origin_y := box.y - widget.canvas.viewport.y;
for * widget.canvas.items {
#if #defined(TK_BITMAP) {
if it.kind == .Image {
image := Items.ItemImage(*widget.canvas, it);
if image != null {
bounds := Items.Bounds(it, *widget.canvas);
DrawBitmap(canvas, box, origin_x+bounds.x0, origin_y+bounds.y0, image, .{});
}
continue;
}
}
if it.kind == .Label {
if it.fill == Items.NONE { continue }
bounds := Items.TextBounds(it);
text_box := Tk.Box.{x = origin_x+bounds.x0, y = origin_y+bounds.y0,
w = bounds.x1-bounds.x0, h = bounds.y1-bounds.y0};
#if #defined(TK_BITMAP) {
font := Fonts.Get(widget.canvas.fonts, it.font);
if font != null {
DrawFontText(*clipped, font, text_box, TextView(it.text), Color(it.fill));
continue;
}
}
DrawText(*clipped, text_box, TextView(it.text), false, Color(it.fill));
continue;
}
if it.kind == .Line {
if it.fill == Items.NONE { continue }
ink := Color(it.fill);
half := Items.Pixel(it.stroke)/2;
for i: 1..it.path.count-1 {
first := it.path[i-1];
next := it.path[i];
DrawLine(*clipped, origin_x+Items.Pixel(first.x), origin_y+Items.Pixel(first.y),
origin_x+Items.Pixel(next.x), origin_y+Items.Pixel(next.y), half, ink);
}
radius := it.marker_radius;
if radius > 0 {
outline := it.fill;
for * it.path {
left := Items.Pixel(it.x-radius);
right := Items.Pixel(it.x+radius);
top := Items.Pixel(it.y-radius);
bottom := Items.Pixel(it.y+radius);
rx := (right-left)/2;
ry := (bottom-top)/2;
DrawOval(*clipped, origin_x+left+rx, origin_y+top+ry, rx, ry, half, Items.NONE, outline);
}
}
continue;
}
rect := Items.Rectangle(it.coordinates);
x := origin_x + rect.x0;
y := origin_y + rect.y0;
w := rect.x1 - rect.x0;
h := rect.y1 - rect.y0;
if it.kind == .Oval {
DrawOval(*clipped, x+w/2, y+h/2, w/2, h/2, Items.Pixel(it.stroke)/2, it.fill, it.outline);
continue;
}
if it.fill != Items.NONE { Fill(*clipped, .{x, y, w, h}, Color(it.fill)) }
if it.outline == Items.NONE { continue }
edge := Color(it.outline);
Fill(*clipped, .{x, y, w+1, 1}, edge);
Fill(*clipped, .{x, y+h, w+1, 1}, edge);
Fill(*clipped, .{x, y, 1, h}, edge);
Fill(*clipped, .{x+w, y, 1, h}, edge);
}
}
Vertex :: struct { x: float64; y: float64; }
CeilPixel :: (value: float64) -> s32 {
pixel := cast(s32)value;
return ifx cast(float64)pixel < value then pixel+1 else pixel;
}
DrawLine :: (canvas: *Canvas, x0: s32, y0: s32, x1: s32, y1: s32, half: s32, ink: Ink) {
dx := cast(float64)(x1-x0);
dy := cast(float64)(y1-y0);
length := Math.Sqrt(dx*dx+dy*dy);
if length == 0.0 {
Fill(canvas, .{x = x0-half, y = y0-half, w = 2*half+1, h = 2*half+1}, ink);
return;
}
radius := cast(float64)half+0.5;
ux := dx/length*radius;
uy := dy/length*radius;
start_x := cast(float64)x0+0.5-ux;
start_y := cast(float64)y0+0.5-uy;
end_x := cast(float64)x1+0.5+ux;
end_y := cast(float64)y1+0.5+uy;
corners: [4]Vertex;
corners[0] = .{start_x-uy, start_y+ux};
corners[1] = .{end_x-uy, end_y+ux};
corners[2] = .{end_x+uy, end_y-ux};
corners[3] = .{start_x+uy, start_y-ux};
clip := canvas.clip;
top := Tk.Maximum(clip.y, Tk.Minimum(y0, y1)-2*half-1);
bottom := Tk.Minimum(clip.y+clip.h-1, Tk.Maximum(y0, y1)+2*half+1);
for y: top..bottom {
row := cast(float64)y+0.5;
left := 2000000.0;
right := -2000000.0;
for i: 0..3 {
p := corners[i];
q := corners[(i+1)%4];
if (p.y <= row && row < q.y) || (q.y <= row && row < p.y) {
at := p.x + (q.x-p.x)*(row-p.y)/(q.y-p.y);
if at < left { left = at }
if at > right { right = at }
}
}
if left <= right {
x := CeilPixel(left-0.5);
Fill(canvas, .{x, y, CeilPixel(right-0.5)-x, 1}, ink);
}
}
}
EllipseSpan :: (rx: s32, ry: s32, offset: s32) -> s32 {
if rx < 0 || ry < 0 || offset < -ry || offset > ry { return -1 }
if ry == 0 { return rx }
fraction := cast(float64)offset/cast(float64)ry;
return cast(s32)(cast(float64)rx*Math.Sqrt(1.0-fraction*fraction)+0.5);
}
DrawOval :: (canvas: *Canvas, x: s32, y: s32, rx: s32, ry: s32, half: s32, fill: s32, outline: s32) {
clip := canvas.clip;
top := Tk.Maximum(clip.y, y-ry-half);
bottom := Tk.Minimum(clip.y+clip.h-1, y+ry+half);
for row: top..bottom {
if fill != Items.NONE {
inside := EllipseSpan(rx, ry, row-y);
if inside >= 0 { Fill(canvas, .{x-inside, row, 2*inside+1, 1}, Color(fill)) }
}
if outline == Items.NONE { continue }
outer := EllipseSpan(rx+half, ry+half, row-y);
inner := EllipseSpan(rx-half-1, ry-half-1, row-y);
if outer < 0 { continue }
if inner < 0 { Fill(canvas, .{x-outer, row, 2*outer+1, 1}, Color(outline)) }
else {
Fill(canvas, .{x-outer, row, outer-inner, 1}, Color(outline));
Fill(canvas, .{x+inner+1, row, outer-inner, 1}, Color(outline));
}
}
}
}
DrawIndicator :: (canvas: *Canvas, x: s32, y: s32, radio: bool, selected: bool, ink: Ink) {
if radio {
for i: 0..12 {
dy := cast(s32)i-6;
span: s32 = 6;
while span*span+dy*dy > 40 { span -= 1 }
Fill(canvas, .{x = x+8-span, y = y+8+dy, w = 2*span+1, h = 1}, BORDER);
span -= 1;
if dy > -6 && dy < 6 { Fill(canvas, .{x = x+8-span, y = y+8+dy, w = 2*span+1, h = 1}, ENTRY) }
if selected && dy >= -3 && dy <= 3 {
dot: s32 = ifx dy == -3 || dy == 3 then 1 else 3;
Fill(canvas, .{x = x+8-dot, y = y+8+dy, w = 2*dot+1, h = 1}, ink);
}
}
return;
}
Fill(canvas, .{x = x, y = y, w = 16, h = 16}, BORDER);
Fill(canvas, .{x = x+1, y = y+1, w = 14, h = 14}, ENTRY);
if selected {
Stroke(canvas, x+3, y+1, "0314", ink);
Stroke(canvas, x+3, y+1, "1441", ink);
}
}
DrawCheck :: (canvas: *Canvas, widget: *Tk.Widget) {
box := widget.box;
ink := ifx widget.disabled then MUTED else LETTER;
selected := Check.Selected(*widget.check);
if widget.check.indicator < 0 {
Fill(canvas, box, BORDER);
Fill(canvas, .{x = box.x+1, y = box.y+1, w = box.w-2, h = box.h-2}, ifx selected then PRESSED else BUTTON);
DrawText(canvas, box, TextView(widget.text), true, ink);
return;
}
if box.w >= 24 && box.h >= 16 {
DrawIndicator(canvas, box.x+6, box.y+(box.h-16)/2, widget.check.radio, selected, ink);
}
DrawText(canvas, .{x = box.x+29, y = box.y, w = box.w-29, h = box.h}, TextView(widget.text), false, ink);
}
ScrollSegment :: (box: Tk.Box, horizontal: bool, first: s32, last: s32, inset: s32 = 0) -> Tk.Box {
if horizontal { return .{x = box.x+first, y = box.y+inset, w = last-first, h = box.h-2*inset} }
return .{x = box.x+inset, y = box.y+first, w = box.w-2*inset, h = last-first};
}
DrawScroll :: (canvas: *Canvas, widget: *Tk.Widget, index: s64) {
box := widget.box;
horizontal := widget.scroll.horizontal;
length := Tk.ScrollLength(widget);
track := Scroll.Track(*widget.scroll, length);
Fill(canvas, box, BORDER);
Fill(canvas, ScrollSegment(box, horizontal, 1, length-1, 1), BUTTON);
Fill(canvas, ScrollSegment(box, horizontal, track.first, track.last), BORDER);
Fill(canvas, ScrollSegment(box, horizontal, track.first+1, track.last-1, 1),
ifx index == Tk.pressed then PRESSED else ENTRY);
for arrow: 0..1 {
center := ifx arrow == 0 then track.arrow/2 else length-1-track.arrow/2;
for row: 0..3 {
spread := cast(s32)row;
major := center + (ifx arrow == 0 then spread-2 else 2-spread);
minor := ifx horizontal then box.h/2 else box.w/2;
if track.arrow < 8 || minor < 4 { continue }
pixels := ifx horizontal then Tk.Box.{x = box.x+major, y = box.y+minor-spread, w = 1, h = 1+2*spread}
else Tk.Box.{x = box.x+minor-spread, y = box.y+major, w = 1+2*spread, h = 1};
Fill(canvas, pixels, LETTER);
}
}
}
DrawScale :: (canvas: *Canvas, widget: *Tk.Widget, index: s64) {
box := widget.box;
track := Tk.ScaleTrack(widget);
Fill(canvas, track, BORDER);
Fill(canvas, .{x = track.x+1, y = track.y+1, w = track.w-2, h = track.h-2}, BUTTON);
offset := Scale.Position(*widget.scale, Tk.ScaleLength(widget));
thumb := track;
if widget.scale.horizontal {
thumb.x += offset;
thumb.w = Tk.Minimum(Scale.SLIDER, track.w-offset);
} else {
thumb.y += offset;
thumb.h = Tk.Minimum(Scale.SLIDER, track.h-offset);
}
Fill(canvas, thumb, ifx index == Tk.focused then LETTER else BORDER);
Fill(canvas, .{x = thumb.x+1, y = thumb.y+1, w = thumb.w-2, h = thumb.h-2},
ifx index == Tk.pressed then PRESSED else ENTRY);
if widget.scale.show_value {
if widget.scale.horizontal { box.h -= track.h } else { box.w -= track.w }
value: Text;
Scale.Append(*value, widget.scale.value);
DrawText(canvas, box, TextView(value));
}
}
DrawPanel :: (canvas: *Canvas, widget: *Tk.Widget) {
panel := *widget.panel;
box := widget.box;
if panel.width == 0 || panel.height == 0 { return }
inner_w := Tk.NonNegative(box.w - 2*panel.border);
inner_h := Tk.NonNegative(box.h - 2*panel.border);
w := Tk.Minimum(inner_w, panel.width);
h := Tk.Minimum(inner_h, panel.height);
x_start := box.x + panel.border + (inner_w-w)/2;
y_start := box.y + panel.border + (inner_h-h)/2;
x_source := (panel.width-w)/2;
y_source := (panel.height-h)/2;
source := VecSlice(panel.pixels, 0, panel.pixels.count);
first := Tk.Maximum(y_start, canvas.first);
end := Tk.Minimum(y_start+h, canvas.height);
for y: first..end-1 {
src := (cast(s64)(y_source+y-y_start) * panel.width + x_source) * 4;
dst := (cast(s64)(y-canvas.first) * canvas.width + x_start) * 4;
for byte: 0..cast(s64)w*4-1 { canvas.pixels[dst+byte] = source[src+byte] }
}
}
// Animated panels can refresh their existing rectangles between layout changes.
RenderPanels :: (pixels: []u8, width: s32, height: s32) {
canvas := Canvas.{pixels = pixels, width = width, height = height};
for * Tk.widgets {
if it.kind == Tk.PANEL && it.order > 0 && it.box.w > 0 && it.box.h > 0 { DrawPanel(*canvas, it) }
}
}