Skip to content

Commit 332b010

Browse files
committed
implemented styled text drawing, added example for it
1 parent c89be98 commit 332b010

File tree

2 files changed

+89
-17
lines changed

2 files changed

+89
-17
lines changed

ansmap.php

Lines changed: 65 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -320,42 +320,87 @@ function ansmap_channel_set_value(&$amp, $channel, $value, $x, $y) {
320320
$amp[$y][$x][$channel] = $value;
321321
}
322322

323-
function ansmap_draw_text(&$amp, $txt, $x, $y, ...$styles) {
323+
function ansmap_draw_text(&$amp, $txt, $x, $y, ...$txt_styles) {
324324
$lines = explode("\n", $txt);
325325
$sym_y = $y;
326326
$decoration = " ";
327327

328-
if (is_array($styles)) {
329-
$cipher = ansmap_channel_get_cipher("styles");
330-
$best_score = null;
331-
$best_key = " ";
328+
$style_to_channel_map = array(
329+
"bold" => "foreground",
330+
"black" => "foreground",
331+
"red" => "foreground",
332+
"green" => "foreground",
333+
"yellow" => "foreground",
334+
"blue" => "foreground",
335+
"magenta" => "foreground",
336+
"cyan" => "foreground",
337+
"white" => "foreground",
338+
"black-bg" => "background",
339+
"red-bg" => "background",
340+
"green-bg" => "background",
341+
"yellow-bg" => "background",
342+
"blue-bg" => "background",
343+
"magenta-bg" => "background",
344+
"cyan-bg" => "background",
345+
"white-bg" => "background",
346+
"hidden" => "decoration",
347+
"faint" => "decoration",
348+
"italic" => "decoration",
349+
"underline" => "decoration",
350+
"blinking" => "decoration",
351+
"strikethrough" => "decoration"
352+
);
332353

333-
if (in_array("hidden", $styles, true)) {
334-
$styles = array("hidden");
354+
$channel_to_styles_map = array();
355+
$channel_to_cipher_key = array();
356+
357+
foreach ($txt_styles as $style) {
358+
if (!array_key_exists($style, $style_to_channel_map)) {
359+
continue;
335360
}
336361

337-
foreach ($cipher as $key => $dict) {
362+
$channel = $style_to_channel_map[$style];
363+
364+
if (!array_key_exists($channel, $channel_to_styles_map)) {
365+
$channel_to_styles_map[$channel] = array();
366+
}
367+
368+
$channel_to_styles_map[$channel][] = $style;
369+
}
370+
371+
if (array_key_exists("decoration", $channel_to_styles_map)
372+
&& in_array("hidden", $channel_to_styles_map["decoration"], true)) {
373+
// No text decoration is needed if the text is hidden anyway.
374+
$channel_to_styles_map["decoration"] = array("hidden");
375+
}
376+
377+
foreach ($channel_to_styles_map as $channel_name => $channel_styles) {
378+
$cipher = ansmap_channel_get_cipher($channel_name);
379+
$best_score = null;
380+
$best_key = " ";
381+
382+
foreach ($cipher as $cipher_key => $cipher_styles) {
338383
$score = 0;
339384

340-
for ($i = 0; $i < count($styles); $i++) {
341-
$var = $styles[$i];
385+
for ($i = 0; $i < count($channel_styles); $i++) {
386+
$var = $channel_styles[$i];
342387

343-
if (array_key_exists($var, $dict)) {
388+
if (in_array($var, $cipher_styles, true)) {
344389
$score++;
345390
}
346391
}
347392

348393
if ($best_score == null || $best_score < $score) {
349-
$best_key = $key;
394+
$best_key = $cipher_key;
350395
$best_score = $score;
351396
}
352397

353-
if ($score >= count($styles)) {
398+
if ($score >= count($channel_styles)) {
354399
break;
355400
}
356401
}
357402

358-
$styles = $best_key;
403+
$channel_to_cipher_key[$channel_name] = $best_key;
359404
}
360405

361406
foreach ($lines as $line) {
@@ -364,9 +409,12 @@ function ansmap_draw_text(&$amp, $txt, $x, $y, ...$styles) {
364409

365410
foreach ($symbols as $sym) {
366411
ansmap_channel_set_value($amp, "symbol", $sym, $sym_x, $sym_y);
367-
ansmap_channel_set_value(
368-
$amp, "decoration", $decoration, $sym_x, $sym_y
369-
);
412+
413+
foreach ($channel_to_cipher_key as $channel_name => $cipher_key) {
414+
ansmap_channel_set_value(
415+
$amp, $channel_name, $cipher_key, $sym_x, $sym_y
416+
);
417+
}
370418

371419
$sym_x++;
372420
}

examples/ex-drawtext.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
require_once("../ansmap.php");
4+
5+
$amp = ansmap_create(20, 6);
6+
7+
ansmap_draw_text(
8+
$amp,
9+
"╔══════════════════╗\n".
10+
"║ ║\n".
11+
"║ ║\n".
12+
"║ ║\n".
13+
"║ ║\n".
14+
"╚══════════════════╝",
15+
0, 0, "blue-bg", "bold", "white"
16+
);
17+
18+
ansmap_draw_text($amp, "B", 8, 2, "bold", "blue", "underline");
19+
ansmap_draw_text($amp, "S", 9, 2, "bold", "yellow", "strikethrough");
20+
ansmap_draw_text($amp, "O", 10, 2, "bold", "cyan", "blinking");
21+
ansmap_draw_text($amp, "D", 11, 2, "bold", "magenta", "italic");
22+
ansmap_draw_text($amp, "╌╌╌╌", 8, 3);
23+
24+
echo ansmap_to_string($amp);

0 commit comments

Comments
 (0)