2021-05-23 11:43:28 +02:00
|
|
|
(*
|
2023-01-13 11:11:45 +01:00
|
|
|
Copyright 2021-2023 Anton Krotov
|
2021-05-23 11:43:28 +02:00
|
|
|
|
|
|
|
This file is part of CEdit.
|
|
|
|
|
|
|
|
CEdit is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
CEdit is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with CEdit. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*)
|
|
|
|
|
|
|
|
MODULE Graph;
|
|
|
|
|
2022-01-24 23:03:13 +01:00
|
|
|
IMPORT SYSTEM, KOSAPI, Lines, Languages, E := Encodings;
|
2021-05-23 11:43:28 +02:00
|
|
|
|
|
|
|
CONST
|
|
|
|
|
2023-03-05 22:40:21 +01:00
|
|
|
modeCOPY = 0;
|
|
|
|
modeNOT = 1;
|
|
|
|
modeXOR = 2;
|
2021-05-23 11:43:28 +02:00
|
|
|
|
2023-03-05 22:40:21 +01:00
|
|
|
triUp* = FALSE;
|
|
|
|
triDown* = TRUE;
|
|
|
|
triLeft* = FALSE;
|
|
|
|
triRight* = TRUE;
|
2021-09-03 01:15:33 +02:00
|
|
|
|
2021-05-23 11:43:28 +02:00
|
|
|
TYPE
|
|
|
|
|
2023-03-05 22:40:21 +01:00
|
|
|
tFont* = POINTER TO RECORD
|
|
|
|
handle*: INTEGER;
|
|
|
|
height*: INTEGER;
|
|
|
|
width*: INTEGER;
|
|
|
|
size: INTEGER;
|
|
|
|
flags: INTEGER;
|
|
|
|
name*: ARRAY 256 OF WCHAR
|
|
|
|
END;
|
|
|
|
|
|
|
|
tCanvas* = POINTER TO RECORD
|
|
|
|
bitmap: INTEGER;
|
|
|
|
width*, height*, sizeY: INTEGER;
|
|
|
|
color, backColor, textColor: INTEGER;
|
|
|
|
font*: tFont;
|
|
|
|
mode: INTEGER
|
|
|
|
END;
|
2021-05-23 11:43:28 +02:00
|
|
|
|
2022-01-24 23:03:13 +01:00
|
|
|
VAR
|
|
|
|
|
2022-01-25 22:57:14 +01:00
|
|
|
fonts*: ARRAY 3 OF tFont;
|
2022-01-24 23:03:13 +01:00
|
|
|
|
2021-05-23 11:43:28 +02:00
|
|
|
|
2021-12-17 07:12:02 +01:00
|
|
|
PROCEDURE getRGB* (color: INTEGER; VAR r, g, b: BYTE);
|
|
|
|
BEGIN
|
|
|
|
b := color MOD 256;
|
|
|
|
g := color DIV 256 MOD 256;
|
|
|
|
r := color DIV 65536 MOD 256
|
|
|
|
END getRGB;
|
|
|
|
|
|
|
|
|
2021-05-23 11:43:28 +02:00
|
|
|
PROCEDURE DrawCanvas* (canvas: tCanvas; x, y: INTEGER);
|
|
|
|
BEGIN
|
2023-03-05 22:40:21 +01:00
|
|
|
KOSAPI.sysfunc7(65, canvas.bitmap, canvas.width*65536 + canvas.height, x*65536 + y, 32, 0, 0)
|
2021-05-23 11:43:28 +02:00
|
|
|
END DrawCanvas;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE SetColor* (canvas: tCanvas; color: INTEGER);
|
|
|
|
BEGIN
|
2023-03-05 22:40:21 +01:00
|
|
|
canvas.color := color
|
2021-05-23 11:43:28 +02:00
|
|
|
END SetColor;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE SetTextColor* (canvas: tCanvas; color: INTEGER);
|
|
|
|
BEGIN
|
2023-03-05 22:40:21 +01:00
|
|
|
canvas.textColor := color
|
2021-05-23 11:43:28 +02:00
|
|
|
END SetTextColor;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE SetBkColor* (canvas: tCanvas; color: INTEGER);
|
|
|
|
BEGIN
|
2023-03-05 22:40:21 +01:00
|
|
|
canvas.backColor := color
|
2021-05-23 11:43:28 +02:00
|
|
|
END SetBkColor;
|
|
|
|
|
|
|
|
|
2022-01-24 23:03:13 +01:00
|
|
|
PROCEDURE CreateFont (size: INTEGER; name: ARRAY OF WCHAR; attr: SET): tFont;
|
2021-05-23 11:43:28 +02:00
|
|
|
VAR
|
2023-03-05 22:40:21 +01:00
|
|
|
font: tFont;
|
2021-05-23 11:43:28 +02:00
|
|
|
BEGIN
|
2022-01-24 23:03:13 +01:00
|
|
|
ASSERT(size IN {0, 1, 2});
|
2023-03-05 22:40:21 +01:00
|
|
|
NEW(font);
|
|
|
|
font.size := size;
|
|
|
|
IF size = 0 THEN
|
|
|
|
font.width := 6;
|
|
|
|
font.height := 9;
|
|
|
|
font.flags := 08000000H
|
|
|
|
ELSE
|
|
|
|
font.width := size*8;
|
|
|
|
font.height := size*16;
|
|
|
|
IF size = 1 THEN
|
|
|
|
font.flags := 28000000H
|
|
|
|
ELSIF size = 2 THEN
|
|
|
|
font.flags := 29000000H
|
|
|
|
END
|
|
|
|
END;
|
|
|
|
font.name := name
|
|
|
|
RETURN font
|
2021-05-23 11:43:28 +02:00
|
|
|
END CreateFont;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE SetFont* (canvas: tCanvas; font: tFont);
|
|
|
|
BEGIN
|
2023-03-05 22:40:21 +01:00
|
|
|
canvas.font := font
|
2021-05-23 11:43:28 +02:00
|
|
|
END SetFont;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE HLine* (canvas: tCanvas; y, x1, x2: INTEGER);
|
|
|
|
VAR
|
2023-03-05 22:40:21 +01:00
|
|
|
X1, X2, i: INTEGER;
|
|
|
|
ptr: INTEGER;
|
|
|
|
color: INTEGER;
|
2021-05-23 11:43:28 +02:00
|
|
|
BEGIN
|
2023-03-05 22:40:21 +01:00
|
|
|
X1 := MAX(MIN(x1, x2), 0);
|
|
|
|
X2 := MIN(MAX(x1, x2), canvas.width - 1);
|
|
|
|
IF (0 <= y) & (y < canvas.height) THEN
|
|
|
|
color := canvas.color;
|
|
|
|
ptr := canvas.bitmap + 4*(y*canvas.width + X1);
|
|
|
|
FOR i := X1 TO X2 DO
|
|
|
|
SYSTEM.PUT32(ptr, color);
|
|
|
|
INC(ptr, 4)
|
|
|
|
END
|
|
|
|
END
|
2021-05-23 11:43:28 +02:00
|
|
|
END HLine;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE VLine* (canvas: tCanvas; x, y1, y2: INTEGER);
|
|
|
|
VAR
|
2023-03-05 22:40:21 +01:00
|
|
|
Y1, Y2, i: INTEGER;
|
|
|
|
ptr: INTEGER;
|
|
|
|
color: INTEGER;
|
2021-05-23 11:43:28 +02:00
|
|
|
BEGIN
|
2023-03-05 22:40:21 +01:00
|
|
|
Y1 := MAX(MIN(y1, y2), 0);
|
|
|
|
Y2 := MIN(MAX(y1, y2), canvas.height - 1);
|
|
|
|
IF (0 <= x) & (x < canvas.width) THEN
|
|
|
|
color := canvas.color;
|
|
|
|
ptr := canvas.bitmap + 4*(Y1*canvas.width + x);
|
|
|
|
FOR i := Y1 TO Y2 DO
|
|
|
|
IF canvas.mode = modeNOT THEN
|
|
|
|
SYSTEM.GET32(ptr, color);
|
|
|
|
color := ORD(-BITS(color)*{0..23})
|
|
|
|
ELSIF canvas.mode = modeXOR THEN
|
|
|
|
SYSTEM.GET32(ptr, color);
|
|
|
|
color := ORD((BITS(color)/BITS(canvas.color))*{0..23})
|
|
|
|
END;
|
|
|
|
SYSTEM.PUT32(ptr, color);
|
|
|
|
INC(ptr, canvas.width*4)
|
|
|
|
END
|
|
|
|
END
|
2021-05-23 11:43:28 +02:00
|
|
|
END VLine;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE notVLine* (canvas: tCanvas; x, y1, y2: INTEGER);
|
|
|
|
BEGIN
|
2023-01-21 13:10:00 +01:00
|
|
|
canvas.mode := modeNOT;
|
|
|
|
VLine(canvas, x, y1, y2);
|
|
|
|
canvas.mode := modeCOPY
|
2021-05-23 11:43:28 +02:00
|
|
|
END notVLine;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE xorVLine* (canvas: tCanvas; x, y1, y2: INTEGER);
|
|
|
|
BEGIN
|
2023-01-21 13:10:00 +01:00
|
|
|
canvas.mode := modeXOR;
|
|
|
|
SetColor(canvas, 0FF0000H);
|
|
|
|
VLine(canvas, x, y1, y2);
|
|
|
|
canvas.mode := modeCOPY
|
2021-05-23 11:43:28 +02:00
|
|
|
END xorVLine;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE DLine* (canvas: tCanvas; x1, x2, y: INTEGER; k: INTEGER);
|
|
|
|
VAR
|
2023-03-05 22:40:21 +01:00
|
|
|
ptr: INTEGER;
|
|
|
|
color: INTEGER;
|
|
|
|
d: INTEGER;
|
2021-05-23 11:43:28 +02:00
|
|
|
BEGIN
|
2021-12-29 16:38:29 +01:00
|
|
|
ASSERT(ABS(k) = 1);
|
2023-03-05 22:40:21 +01:00
|
|
|
color := canvas.color;
|
|
|
|
ptr := canvas.bitmap + 4*(y*canvas.width + x1);
|
|
|
|
d := 4*(1 - canvas.width*k);
|
|
|
|
WHILE x1 <= x2 DO
|
|
|
|
SYSTEM.PUT32(ptr, color);
|
|
|
|
INC(ptr, d);
|
|
|
|
INC(x1)
|
|
|
|
END
|
2021-05-23 11:43:28 +02:00
|
|
|
END DLine;
|
|
|
|
|
|
|
|
|
2021-09-03 01:15:33 +02:00
|
|
|
PROCEDURE Triangle* (canvas: tCanvas; x1, y1, x2, y2: INTEGER; orientation: BOOLEAN);
|
|
|
|
VAR
|
|
|
|
i, a, b, d: INTEGER;
|
2021-09-28 18:15:37 +02:00
|
|
|
line: PROCEDURE (canvas: tCanvas; c, c1, c2: INTEGER);
|
2021-09-03 01:15:33 +02:00
|
|
|
BEGIN
|
2021-09-28 18:15:37 +02:00
|
|
|
line := NIL;
|
2021-09-03 01:15:33 +02:00
|
|
|
d := ORD(orientation)*2 - 1;
|
|
|
|
IF y1 = y2 THEN
|
|
|
|
i := y1;
|
|
|
|
a := MIN(x1, x2);
|
|
|
|
b := MAX(x1, x2);
|
2021-09-28 18:15:37 +02:00
|
|
|
line := HLine
|
2021-09-03 01:15:33 +02:00
|
|
|
ELSIF x1 = x2 THEN
|
|
|
|
i := x1;
|
|
|
|
a := MIN(y1, y2);
|
|
|
|
b := MAX(y1, y2);
|
2021-09-28 18:15:37 +02:00
|
|
|
line := VLine
|
|
|
|
END;
|
|
|
|
IF line # NIL THEN
|
2021-09-03 01:15:33 +02:00
|
|
|
WHILE a <= b DO
|
2021-09-28 18:15:37 +02:00
|
|
|
line(canvas, i, a, b);
|
2021-09-03 01:15:33 +02:00
|
|
|
INC(i, d);
|
|
|
|
INC(a);
|
|
|
|
DEC(b)
|
|
|
|
END
|
|
|
|
END
|
|
|
|
END Triangle;
|
|
|
|
|
|
|
|
|
2021-05-23 11:43:28 +02:00
|
|
|
PROCEDURE FillRect* (canvas: tCanvas; left, top, right, bottom: INTEGER);
|
|
|
|
VAR
|
2023-03-05 22:40:21 +01:00
|
|
|
y: INTEGER;
|
2021-05-23 11:43:28 +02:00
|
|
|
BEGIN
|
2023-03-05 22:40:21 +01:00
|
|
|
FOR y := top TO bottom DO
|
|
|
|
HLine(canvas, y, left, right)
|
|
|
|
END
|
2021-05-23 11:43:28 +02:00
|
|
|
END FillRect;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE Rect* (canvas: tCanvas; left, top, right, bottom: INTEGER);
|
|
|
|
BEGIN
|
2023-03-05 22:40:21 +01:00
|
|
|
HLine(canvas, top, left, right);
|
|
|
|
HLine(canvas, bottom, left, right);
|
|
|
|
VLine(canvas, left, top, bottom);
|
|
|
|
VLine(canvas, right, top, bottom)
|
2021-05-23 11:43:28 +02:00
|
|
|
END Rect;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE clear* (canvas: tCanvas);
|
|
|
|
VAR
|
2023-03-05 22:40:21 +01:00
|
|
|
ptr, ptr2, w, i: INTEGER;
|
2021-05-23 11:43:28 +02:00
|
|
|
BEGIN
|
2023-03-05 22:40:21 +01:00
|
|
|
HLine(canvas, 0, 0, canvas.width - 1);
|
|
|
|
w := canvas.width*4;
|
|
|
|
ptr := canvas.bitmap;
|
|
|
|
ptr2 := ptr;
|
|
|
|
i := canvas.sizeY - 1;
|
|
|
|
WHILE i > 0 DO
|
|
|
|
INC(ptr2, w);
|
|
|
|
SYSTEM.MOVE(ptr, ptr2, w);
|
|
|
|
DEC(i)
|
|
|
|
END
|
2021-05-23 11:43:28 +02:00
|
|
|
END clear;
|
|
|
|
|
|
|
|
|
2021-09-15 23:40:33 +02:00
|
|
|
PROCEDURE TextOut* (canvas: tCanvas; x, y: INTEGER; text: INTEGER; n: INTEGER; delimColor: INTEGER);
|
2021-05-23 11:43:28 +02:00
|
|
|
CONST
|
2023-03-05 22:40:21 +01:00
|
|
|
WCHAR_SIZE = 2;
|
2021-05-23 11:43:28 +02:00
|
|
|
VAR
|
2023-03-05 22:40:21 +01:00
|
|
|
color, i: INTEGER;
|
|
|
|
font: tFont;
|
|
|
|
c: WCHAR;
|
2021-05-23 11:43:28 +02:00
|
|
|
BEGIN
|
2022-01-24 23:03:13 +01:00
|
|
|
font := canvas.font;
|
2023-03-05 22:40:21 +01:00
|
|
|
IF (0 <= y) & (y <= canvas.sizeY - font.height - 1) THEN
|
|
|
|
IF x < 0 THEN
|
|
|
|
i := -(x DIV font.width);
|
|
|
|
INC(x, i*font.width);
|
|
|
|
DEC(n, i)
|
|
|
|
ELSE
|
|
|
|
i := 0
|
|
|
|
END;
|
|
|
|
IF n > 0 THEN
|
|
|
|
n := MAX(MIN(n, (canvas.width - x) DIV font.width), 0);
|
|
|
|
color := canvas.color;
|
|
|
|
canvas.color := canvas.backColor;
|
|
|
|
FillRect(canvas, x, y, x + n*font.width - 1, y + font.height);
|
|
|
|
canvas.color := color;
|
|
|
|
WHILE n > 0 DO
|
|
|
|
SYSTEM.GET(text + i*WCHAR_SIZE, c);
|
|
|
|
IF ~Lines.isSpace(c) THEN
|
|
|
|
IF Languages.isDelim(c) THEN
|
|
|
|
color := delimColor
|
|
|
|
ELSE
|
|
|
|
color := canvas.textColor
|
|
|
|
END;
|
2022-01-02 19:57:13 +01:00
|
|
|
IF c = Lines.NUL THEN
|
2023-03-05 22:40:21 +01:00
|
|
|
c := 0X
|
2022-01-02 19:57:13 +01:00
|
|
|
END;
|
2022-01-25 22:57:14 +01:00
|
|
|
IF font = fonts[0] THEN
|
2023-02-22 16:56:05 +01:00
|
|
|
c := WCHR(E.UNI[ORD(c), E.CP866])
|
2022-01-24 23:03:13 +01:00
|
|
|
END;
|
2023-03-05 22:40:21 +01:00
|
|
|
KOSAPI.sysfunc6(4, x*65536 + y, font.flags + color, SYSTEM.ADR(c), 1, canvas.bitmap - 8)
|
|
|
|
END;
|
|
|
|
INC(x, font.width);
|
|
|
|
INC(i);
|
|
|
|
DEC(n)
|
|
|
|
END
|
|
|
|
END
|
|
|
|
END
|
2021-05-23 11:43:28 +02:00
|
|
|
END TextOut;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE TextOut2* (canvas: tCanvas; x, y: INTEGER; text: ARRAY OF WCHAR; n: INTEGER);
|
|
|
|
BEGIN
|
2023-03-05 22:40:21 +01:00
|
|
|
TextOut(canvas, x, y, SYSTEM.ADR(text[0]), n, canvas.textColor)
|
2021-05-23 11:43:28 +02:00
|
|
|
END TextOut2;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE CreateCanvas* (width, height: INTEGER): tCanvas;
|
|
|
|
VAR
|
2023-03-05 22:40:21 +01:00
|
|
|
canvas: tCanvas;
|
2021-05-23 11:43:28 +02:00
|
|
|
BEGIN
|
2023-03-05 22:40:21 +01:00
|
|
|
NEW(canvas);
|
|
|
|
canvas.bitmap := KOSAPI.malloc(width*height*4 + 8);
|
|
|
|
ASSERT(canvas.bitmap # 0);
|
|
|
|
SYSTEM.PUT32(canvas.bitmap, width);
|
|
|
|
SYSTEM.PUT32(canvas.bitmap + 4, height);
|
|
|
|
INC(canvas.bitmap, 8);
|
|
|
|
canvas.width := width;
|
|
|
|
canvas.height := height;
|
|
|
|
canvas.sizeY := height;
|
|
|
|
canvas.mode := modeCOPY
|
|
|
|
RETURN canvas
|
2021-05-23 11:43:28 +02:00
|
|
|
END CreateCanvas;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE destroy* (VAR canvas: tCanvas);
|
|
|
|
BEGIN
|
2023-03-05 22:40:21 +01:00
|
|
|
IF canvas # NIL THEN
|
|
|
|
canvas.bitmap := KOSAPI.free(canvas.bitmap - 8);
|
|
|
|
DISPOSE(canvas)
|
|
|
|
END
|
2021-05-23 11:43:28 +02:00
|
|
|
END destroy;
|
|
|
|
|
|
|
|
|
2022-01-24 23:03:13 +01:00
|
|
|
BEGIN
|
2023-03-05 22:40:21 +01:00
|
|
|
fonts[0] := CreateFont(0, "", {});
|
|
|
|
fonts[1] := CreateFont(1, "", {});
|
|
|
|
fonts[2] := CreateFont(2, "", {});
|
2021-05-23 11:43:28 +02:00
|
|
|
END Graph.
|