improve text drawing code to work with multiple font sizes (no UI)

This commit is contained in:
cy384 2021-06-01 21:32:51 -04:00
parent 5ee3e7104b
commit 9a7b95ef6f
4 changed files with 51 additions and 86 deletions

View File

@ -20,6 +20,8 @@
char key_to_vterm[256] = { VTERM_KEY_NONE };
int font_offset = 0;
void setup_key_translation(void)
{
// TODO: figure out how to translate the rest of these
@ -57,11 +59,10 @@ void setup_key_translation(void)
//key_to_vterm[0] = VTERM_KEY_KP_EQUAL;
}
Rect cell_rect(int x, int y, Rect bounds)
inline Rect cell_rect(int x, int y, Rect bounds)
{
Rect r = { (short) (bounds.top + y * con.cell_height), (short) (bounds.left + x * con.cell_width + 2),
(short) (bounds.top + (y+1) * con.cell_height), (short) (bounds.left + (x+1) * con.cell_width + 2) };
Rect r = { (short) (bounds.top + y * con.cell_height + 2), (short) (bounds.left + x * con.cell_width + 2),
(short) (bounds.top + (y+1) * con.cell_height + 2), (short) (bounds.left + (x+1) * con.cell_width + 2) };
return r;
}
@ -72,7 +73,7 @@ void print_string(const char* c)
inline void draw_char(int x, int y, Rect* r, char c)
{
MoveTo(r->left + x * con.cell_width + 2, r->top + ((y+1) * con.cell_height) - 2);
MoveTo(r->left + 2 + x * con.cell_width, r->top + 2 - font_offset + ((y+1) * con.cell_height));
DrawChar(c);
}
@ -111,46 +112,7 @@ int qd2idx(int qdc)
}
// convert vterm's ANSI color indexes into Quickdraw colors
inline int idx2qd(VTermColor c)
{
switch (c.indexed.idx)
{
case 0:
return blackColor;
case 1:
return redColor;
case 2:
return greenColor;
case 3:
return yellowColor;
case 4:
return blueColor;
case 5:
return magentaColor;
case 6:
return cyanColor;
case 7:
return whiteColor;
case 8:
return blackColor;
case 9:
return redColor;
case 10:
return greenColor;
case 11:
return yellowColor;
case 12:
return blueColor;
case 13:
return magentaColor;
case 14:
return cyanColor;
case 15:
return whiteColor;
default:
return blackColor;
}
}
int idx2qd[16] = { blackColor, redColor, greenColor, yellowColor, blueColor, magentaColor, cyanColor, whiteColor, blackColor, redColor, greenColor, yellowColor, blueColor, magentaColor, cyanColor, whiteColor };
void point_to_cell(Point p, int* x, int* y)
{
@ -403,11 +365,6 @@ void draw_screen_color(Rect* r)
//short minCol = (0 > (r->left - bounds.left) / con.cell_width) ? 0 : (r->left - bounds.left) / con.cell_width;
//short maxCol = (con.size_x < (r->right - bounds.left + con.cell_width - 1) / con.cell_width) ? con.size_x : (r->right - bounds.left + con.cell_width - 1) / con.cell_width;
short minRow = 0;
short maxRow = con.size_y;
short minCol = 0;
short maxCol = con.size_x;
// don't clobber font settings
short save_font = qd.thePort->txFont;
short save_font_size = qd.thePort->txSize;
@ -415,15 +372,19 @@ void draw_screen_color(Rect* r)
short save_font_fg = qd.thePort->fgColor;
short save_font_bg = qd.thePort->bkColor;
TextFont(kFontIDMonaco);
TextSize(9);
TextFace(normal);
qd.thePort->bkColor = prefs.bg_color;
qd.thePort->fgColor = prefs.fg_color;
short minRow = 0;
short maxRow = con.size_y;
short minCol = 0;
short maxCol = con.size_x;
TextFont(kFontIDMonaco);
TextSize(9);
TextSize(prefs.font_size);
TextFace(normal);
BackColor(prefs.bg_color);
ForeColor(prefs.fg_color);
//EraseRect(&con.offscreen->portRect);
TextMode(srcOr);
short face = normal;
@ -468,13 +429,13 @@ void draw_screen_color(Rect* r)
if (vtsc->pen.reverse)
{
qd.thePort->bkColor = idx2qd(vtsc->pen.fg);
qd.thePort->fgColor = idx2qd(vtsc->pen.bg);
BackColor(idx2qd[vtsc->pen.fg.indexed.idx]);
ForeColor(idx2qd[vtsc->pen.bg.indexed.idx]);
}
else
{
qd.thePort->fgColor = idx2qd(vtsc->pen.fg);
qd.thePort->bkColor = idx2qd(vtsc->pen.bg);
ForeColor(idx2qd[vtsc->pen.fg.indexed.idx]);
BackColor(idx2qd[vtsc->pen.bg.indexed.idx]);
}
cr = cell_rect(pos.col, pos.row, *r);
@ -495,9 +456,8 @@ void draw_screen_color(Rect* r)
if (vtsc->pen.italic) face |= (condense|italic);
if (vtsc->pen.underline) face |= underline;
if (face != normal) TextFace(face);
TextFace(face);
draw_char(pos.col, pos.row, r, c);
if (face != normal) TextFace(normal);
if (i < select_end && i >= select_start)
{
@ -543,17 +503,11 @@ void draw_screen_fast(Rect* r)
short save_font_bg = qd.thePort->bkColor;
TextFont(kFontIDMonaco);
TextSize(9);
TextSize(prefs.font_size);
TextFace(normal);
qd.thePort->bkColor = whiteColor;
qd.thePort->fgColor = blackColor;
//EraseRect(r);
TextFont(kFontIDMonaco);
TextSize(9);
TextFace(normal);
TextMode(srcOr);
TextMode(srcOr); // or mode is faster for drawing black on white
int select_start = -1;
int select_end = -1;
@ -588,6 +542,8 @@ void draw_screen_fast(Rect* r)
char row_invert[con.size_x];
Rect cr;
int vertical_offset = r->top + con.cell_height - font_offset + 2;
for(pos.row = 0; pos.row < con.size_y; pos.row++)
{
erase_row(pos.row);
@ -601,7 +557,7 @@ void draw_screen_fast(Rect* r)
i++;
}
MoveTo(r->left + 2, r->top + ((pos.row+1) * con.cell_height) - 2);
MoveTo(r->left + 2, vertical_offset);
DrawText(row_text, 0, con.size_x);
for (int i = 0; i < con.size_x; i++)
@ -612,6 +568,7 @@ void draw_screen_fast(Rect* r)
InvertRect(&cr);
}
}
vertical_offset += con.cell_height;
}
// do the cursor if needed
@ -814,21 +771,26 @@ void console_setup(void)
con.size_y = 24;
TextFont(kFontIDMonaco);
TextSize(9);
TextSize(prefs.font_size);
TextFace(normal);
con.cell_height = 12;
con.cell_width = CharWidth('M');
FontInfo fi = {0};
GetFontInfo(&fi);
con.cell_height = fi.ascent + fi.descent + fi.leading + 1;
font_offset = fi.descent;
con.cell_width = fi.widMax;
TextFont(save_font);
TextSize(save_font_size);
TextFace(save_font_face);
Rect initial_window_bounds = qd.screenBits.bounds;
InsetRect(&initial_window_bounds, 20, 20);
initial_window_bounds.top += 40;
initial_window_bounds.bottom = initial_window_bounds.top + con.cell_height * con.size_y + 2;
initial_window_bounds.bottom = initial_window_bounds.top + con.cell_height * con.size_y + 4;
initial_window_bounds.right = initial_window_bounds.left + con.cell_width * con.size_x + 4;
ConstStr255Param title = "\pssheven " SSHEVEN_VERSION;

View File

@ -4,11 +4,11 @@
#define __SSHEVEN_CONSTANTS_R__
/* so many versions */
#define SSHEVEN_VERSION "0.8.6"
#define SSHEVEN_LONG_VERSION "0.8.6 prerelease, by cy384"
#define SSHEVEN_DESCRIPTION "ssheven 0.8.6 by cy384"
#define SSHEVEN_VERSION "0.8.7"
#define SSHEVEN_LONG_VERSION "0.8.7 prerelease, by cy384"
#define SSHEVEN_DESCRIPTION "ssheven 0.8.7 by cy384"
#define SSHEVEN_VERSION_MAJOR 0x00
#define SSHEVEN_VERSION_MINOR 0x86
#define SSHEVEN_VERSION_MINOR 0x87
#define SSHEVEN_VERSION_PRERELEASE 0x01
/* options: development, alpha, beta, release */

View File

@ -131,7 +131,7 @@ int save_prefs(void)
memset(output_buffer, 0, write_length);
long int i = snprintf(output_buffer, write_length, "%d\n%d\n", prefs.major_version, prefs.minor_version);
i += snprintf(output_buffer+i, write_length-i, "%d\n%d\n%d\n%d\n", (int)prefs.auth_type, (int)prefs.display_mode, (int)prefs.fg_color, (int)prefs.bg_color);
i += snprintf(output_buffer+i, write_length-i, "%d\n%d\n%d\n%d\n%d\n", (int)prefs.auth_type, (int)prefs.display_mode, (int)prefs.fg_color, (int)prefs.bg_color, (int)prefs.font_size);
snprintf(output_buffer+i, prefs.hostname[0]+1, "%s", prefs.hostname+1); i += prefs.hostname[0];
i += snprintf(output_buffer+i, write_length-i, "\n");
@ -192,6 +192,7 @@ void init_prefs(void)
prefs.display_mode = COLOR;
prefs.fg_color = blackColor;
prefs.bg_color = whiteColor;
prefs.font_size = 9;
prefs.loaded_from_file = 0;
}
@ -241,7 +242,7 @@ void load_prefs(void)
if ((prefs.major_version == SSHEVEN_VERSION_MAJOR) && (prefs.minor_version == SSHEVEN_VERSION_MINOR))
{
prefs.loaded_from_file = 1;
items_got = sscanf(buffer, "%d\n%d\n%d\n%d\n%d\n%d\n%255[^\n]\n%255[^\n]\n%255[^\n]\n%[^\n]\n%[^\n]", &prefs.major_version, &prefs.minor_version, (int*)&prefs.auth_type, (int*)&prefs.display_mode, &prefs.fg_color, &prefs.bg_color, prefs.hostname+1, prefs.username+1, prefs.port+1, prefs.privkey_path, prefs.pubkey_path);
items_got = sscanf(buffer, "%d\n%d\n%d\n%d\n%d\n%d\n%d\n%255[^\n]\n%255[^\n]\n%255[^\n]\n%[^\n]\n%[^\n]", &prefs.major_version, &prefs.minor_version, (int*)&prefs.auth_type, (int*)&prefs.display_mode, &prefs.fg_color, &prefs.bg_color, &prefs.font_size, prefs.hostname+1, prefs.username+1, prefs.port+1, prefs.privkey_path, prefs.pubkey_path);
// add the size for the pascal strings
prefs.hostname[0] = (unsigned char)strlen(prefs.hostname+1);
@ -478,7 +479,7 @@ void resize_con_window(WindowPtr eventWin, EventRecord event)
int width = growResult & 0xFFFF;
// 'snap' to a size that won't have extra pixels not in a cell
int next_height = height - ((height - 2) % con.cell_height);
int next_height = height - ((height - 4) % con.cell_height);
int next_width = width - ((width - 4) % con.cell_width);
SizeWindow(eventWin, next_width, next_height, true);
@ -567,12 +568,12 @@ void event_loop(void)
// wait to get a GUI event
while (!WaitNextEvent(everyEvent, &event, sleep_time, NULL))
{
// timed out without any GUI events
// toggle our cursor if needed
YieldToAnyThread();
check_cursor();
// then let any other threads run before we wait for events again
YieldToAnyThread();
BeginUpdate(con.win);
draw_screen(&(con.win->portRect));
EndUpdate(con.win);
}
// might need to toggle our cursor even if we got an event

View File

@ -90,6 +90,8 @@ struct preferences
enum { FASTEST, COLOR } display_mode;
int fg_color;
int bg_color;
int font_size;
};
extern struct preferences prefs;