| 1 |
#include "Plotter.hh" |
| 2 |
|
| 3 |
#include "FileOperations.hh" |
| 4 |
#include "IntegerSetting.hh" |
| 5 |
#include "MSXCliComm.hh" |
| 6 |
#include "MSXMotherBoard.hh" |
| 7 |
#include "PNG.hh" |
| 8 |
#include "PlotterFont.hh" |
| 9 |
|
| 10 |
#include "ScopedAssign.hh" |
| 11 |
#include "gl_mat.hh" |
| 12 |
#include "ranges.hh" |
| 13 |
#include "serialize.hh" |
| 14 |
#include "strCat.hh" |
| 15 |
|
| 16 |
#include <algorithm> |
| 17 |
#include <cmath> |
| 18 |
|
| 19 |
namespace openmsx { |
| 20 |
|
| 21 |
|
| 22 |
#if 1 |
| 23 |
template<typename... Args> |
| 24 |
static void printDebug(Args&&...) |
| 25 |
{ |
| 26 |
|
| 27 |
} |
| 28 |
#else |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
#endif |
| 35 |
|
| 36 |
MSXPlotter::MSXPlotter(MSXMotherBoard& motherBoard) |
| 37 |
: cliComm(motherBoard.getMSXCliComm()) |
| 38 |
, dpiSetting(motherBoard.getSharedStuff<IntegerSetting>( |
| 39 |
"print-resolution", |
| 40 |
motherBoard.getCommandController(), "print-resolution", |
| 41 |
"resolution of the output image of emulated dot matrix printer in DPI", |
| 42 |
300, 72, 1200)) |
| 43 |
, charSetSetting( |
| 44 |
motherBoard.getCommandController(), |
| 45 |
"plotter-charset", |
| 46 |
"character set for the MSX plotter", |
| 47 |
CharacterSet::International, |
| 48 |
EnumSetting<CharacterSet>::Map{ |
| 49 |
{"international", CharacterSet::International}, |
| 50 |
{"din", CharacterSet::DIN}}) |
| 51 |
, dipSwitch4Setting( |
| 52 |
motherBoard.getCommandController(), |
| 53 |
"plotter-dipswitch4", |
| 54 |
"dipswitch 4 setting for the MSX plotter", |
| 55 |
false) |
| 56 |
, penThicknessSetting( |
| 57 |
motherBoard.getCommandController(), |
| 58 |
"plotter-pen-thickness", |
| 59 |
"pen thickness for the MSX plotter", |
| 60 |
PenThickness::Standard, |
| 61 |
EnumSetting<PenThickness>::Map{ |
| 62 |
{"standard", PenThickness::Standard}, |
| 63 |
{"thick", PenThickness::Thick}}) |
| 64 |
{ |
| 65 |
resetSettings(); |
| 66 |
ensurePrintPage(); |
| 67 |
} |
| 68 |
|
| 69 |
MSXPlotter::~MSXPlotter() |
| 70 |
{ |
| 71 |
flushEmulatedPrinter(); |
| 72 |
} |
| 73 |
|
| 74 |
zstring_view MSXPlotter::getName() const |
| 75 |
{ |
| 76 |
return "MSXPlotter"; |
| 77 |
} |
| 78 |
|
| 79 |
void MSXPlotter::plugHelper(Connector& connector_, EmuTime time) |
| 80 |
{ |
| 81 |
PrinterCore::plugHelper(connector_, time); |
| 82 |
resetSettings(); |
| 83 |
ensurePrintPage(); |
| 84 |
} |
| 85 |
|
| 86 |
zstring_view MSXPlotter::getDescription() const |
| 87 |
{ |
| 88 |
return "Sony PRN-C41 Color Plotter (4 pens)"; |
| 89 |
} |
| 90 |
|
| 91 |
MSXPlotter::CharacterSet MSXPlotter::getCharacterSet() const |
| 92 |
{ |
| 93 |
return charSetSetting.getEnum(); |
| 94 |
} |
| 95 |
|
| 96 |
void MSXPlotter::setCharacterSet(CharacterSet cs) |
| 97 |
{ |
| 98 |
charSetSetting.setEnum(cs); |
| 99 |
} |
| 100 |
|
| 101 |
bool MSXPlotter::getDipSwitch4() const |
| 102 |
{ |
| 103 |
return dipSwitch4Setting.getBoolean(); |
| 104 |
} |
| 105 |
|
| 106 |
void MSXPlotter::setDipSwitch4(bool enabled) |
| 107 |
{ |
| 108 |
dipSwitch4Setting.setBoolean(enabled); |
| 109 |
} |
| 110 |
|
| 111 |
void MSXPlotter::cyclePen() |
| 112 |
{ |
| 113 |
selectedPen = (selectedPen + 1) % 4; |
| 114 |
} |
| 115 |
|
| 116 |
void MSXPlotter::moveStep(gl::vec2 delta) |
| 117 |
{ |
| 118 |
gl::vec2 oldAbsPos = penPosition + origin; |
| 119 |
gl::vec2 newAbsPos = gl::clamp(oldAbsPos + delta, gl::vec2{0.0f}, PLOT_AREA); |
| 120 |
penPosition = newAbsPos - origin; |
| 121 |
} |
| 122 |
|
| 123 |
void MSXPlotter::ejectPaper() |
| 124 |
{ |
| 125 |
flushEmulatedPrinter(); |
| 126 |
resetSettings(); |
| 127 |
ensurePrintPage(); |
| 128 |
} |
| 129 |
|
| 130 |
void MSXPlotter::write(uint8_t data) |
| 131 |
{ |
| 132 |
printDebug("Plotter: received 0x", hex_string<2>(data), |
| 133 |
" mode=", (mode == Mode::TEXT ? "TEXT" : "GRAPHIC"), |
| 134 |
" escState=", int(escState)); |
| 135 |
|
| 136 |
|
| 137 |
switch (escState) { |
| 138 |
using enum EscState; |
| 139 |
case ESC: |
| 140 |
if (data == '#') { |
| 141 |
|
| 142 |
printDebug("Plotter: entering GRAPHIC mode"); |
| 143 |
mode = Mode::GRAPHIC; |
| 144 |
graphicCmdBuffer.clear(); |
| 145 |
escState = NONE; |
| 146 |
} else if (data == '$') { |
| 147 |
|
| 148 |
printDebug("Plotter: returning to TEXT mode"); |
| 149 |
if (!graphicCmdBuffer.empty()) { |
| 150 |
executeGraphicCommand(); |
| 151 |
graphicCmdBuffer.clear(); |
| 152 |
} |
| 153 |
liftPen(); |
| 154 |
mode = Mode::TEXT; |
| 155 |
escState = NONE; |
| 156 |
} else if (data == 'C') { |
| 157 |
|
| 158 |
escState = ESC_C; |
| 159 |
} else { |
| 160 |
|
| 161 |
cliComm.printWarning( |
| 162 |
"Plotter: unknown ESC sequence 0x", hex_string<2>(data)); |
| 163 |
escState = NONE; |
| 164 |
} |
| 165 |
break; |
| 166 |
|
| 167 |
case ESC_C: |
| 168 |
if ('0' <= data && data <= '3') { |
| 169 |
liftPen(); |
| 170 |
selectedPen = data - '0'; |
| 171 |
printDebug("Plotter: selected pen ", selectedPen); |
| 172 |
terminatorSkip = TerminatorSkip::START; |
| 173 |
} |
| 174 |
escState = NONE; |
| 175 |
break; |
| 176 |
|
| 177 |
case EscState::ESC_S: |
| 178 |
if (data <= 15) { |
| 179 |
|
| 180 |
charScale = data; |
| 181 |
updateLineFeed(); |
| 182 |
printDebug("Plotter: text scale set to ", charScale); |
| 183 |
terminatorSkip = TerminatorSkip::START; |
| 184 |
escState = NONE; |
| 185 |
} else if ('0' <= data && data <= '9') { |
| 186 |
|
| 187 |
unsigned val = data - '0'; |
| 188 |
if (val == 0 || val >= 2) { |
| 189 |
|
| 190 |
charScale = val; |
| 191 |
updateLineFeed(); |
| 192 |
printDebug("Plotter: text scale set to ", charScale); |
| 193 |
terminatorSkip = TerminatorSkip::START; |
| 194 |
escState = NONE; |
| 195 |
} else { |
| 196 |
|
| 197 |
pendingScaleDigit = val; |
| 198 |
escState = ESC_S_EXP_DIGIT; |
| 199 |
} |
| 200 |
} else { |
| 201 |
|
| 202 |
escState = NONE; |
| 203 |
processTextMode(data); |
| 204 |
} |
| 205 |
break; |
| 206 |
|
| 207 |
case ESC_S_EXP_DIGIT: |
| 208 |
if ('0' <= data && data <= '9') { |
| 209 |
unsigned val = pendingScaleDigit * 10 + (data - '0'); |
| 210 |
if (val <= 15) { |
| 211 |
charScale = val; |
| 212 |
updateLineFeed(); |
| 213 |
printDebug("Plotter: text scale set to ", charScale); |
| 214 |
terminatorSkip = TerminatorSkip::START; |
| 215 |
escState = NONE; |
| 216 |
} else { |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
escState = NONE; |
| 221 |
processTextMode(data); |
| 222 |
} |
| 223 |
} else { |
| 224 |
|
| 225 |
|
| 226 |
charScale = pendingScaleDigit; |
| 227 |
updateLineFeed(); |
| 228 |
printDebug("Plotter: text scale set to ", charScale); |
| 229 |
terminatorSkip = TerminatorSkip::START; |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
escState = NONE; |
| 236 |
processTextMode(data); |
| 237 |
} |
| 238 |
break; |
| 239 |
|
| 240 |
case NONE: |
| 241 |
|
| 242 |
if (mode == Mode::TEXT) { |
| 243 |
processTextMode(data); |
| 244 |
} else { |
| 245 |
processGraphicMode(data); |
| 246 |
} |
| 247 |
break; |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
void MSXPlotter::forceFormFeed() |
| 252 |
{ |
| 253 |
flushEmulatedPrinter(); |
| 254 |
} |
| 255 |
|
| 256 |
void MSXPlotter::processTextMode(uint8_t data) |
| 257 |
{ |
| 258 |
using enum TerminatorSkip; |
| 259 |
if (terminatorSkip != NONE) { |
| 260 |
if (data == 0x0D) { |
| 261 |
terminatorSkip = SEEN_CR; |
| 262 |
return; |
| 263 |
} else if (data == 0x0A) { |
| 264 |
terminatorSkip = NONE; |
| 265 |
return; |
| 266 |
} else { |
| 267 |
terminatorSkip = NONE; |
| 268 |
|
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
if (printNext) { |
| 273 |
|
| 274 |
|
| 275 |
drawCharacter(uint8_t(data - 64)); |
| 276 |
printNext = false; |
| 277 |
return; |
| 278 |
} |
| 279 |
|
| 280 |
switch (data) { |
| 281 |
case 0x01: |
| 282 |
printNext = true; |
| 283 |
break; |
| 284 |
case 0x08: |
| 285 |
penPosition.x = std::max(penPosition.x - float(8 + 2 * charScale), 0.0f); |
| 286 |
break; |
| 287 |
case 0x0d: |
| 288 |
penPosition.x = 0.0f; |
| 289 |
if (!getDipSwitch4()) break; |
| 290 |
[[fallthrough]]; |
| 291 |
case 0x0a: |
| 292 |
penPosition.y -= lineFeed; |
| 293 |
if (penPosition.y < -1354.0f) { |
| 294 |
flushEmulatedPrinter(); |
| 295 |
ensurePrintPage(); |
| 296 |
penPosition.y = 30.0f; |
| 297 |
} |
| 298 |
break; |
| 299 |
case 0x0b: |
| 300 |
penPosition.y = std::min(penPosition.y + lineFeed, 30.0f); |
| 301 |
break; |
| 302 |
case 0x0C: |
| 303 |
flushEmulatedPrinter(); |
| 304 |
ensurePrintPage(); |
| 305 |
penPosition = {0.0f, 30.0f}; |
| 306 |
break; |
| 307 |
case 0x12: |
| 308 |
escState = EscState::ESC_S; |
| 309 |
break; |
| 310 |
case 0x1b: |
| 311 |
escState = EscState::ESC; |
| 312 |
break; |
| 313 |
default: |
| 314 |
|
| 315 |
if (data >= 32) { |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
if (penPosition.x >= RIGHT_BORDER) { |
| 320 |
|
| 321 |
penPosition.x = 0.0f; |
| 322 |
penPosition.y -= lineFeed; |
| 323 |
if (penPosition.y < -1354.0f) { |
| 324 |
flushEmulatedPrinter(); |
| 325 |
ensurePrintPage(); |
| 326 |
penPosition.y = 30.0f; |
| 327 |
} |
| 328 |
} |
| 329 |
drawCharacter(data); |
| 330 |
} |
| 331 |
break; |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
void MSXPlotter::processGraphicMode(uint8_t data) |
| 336 |
{ |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
if (data == one_of(0x0d, 0x0a)) { |
| 342 |
|
| 343 |
executeGraphicCommand(); |
| 344 |
} else if (data == 0x1b) { |
| 345 |
|
| 346 |
executeGraphicCommand(); |
| 347 |
escState = EscState::ESC; |
| 348 |
} else if (32 <= data && data < 127) { |
| 349 |
|
| 350 |
graphicCmdBuffer += char(data); |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
if (graphicCmdBuffer.size() > 1) { |
| 356 |
|
| 357 |
bool isPrintCmd = (graphicCmdBuffer[0] == 'P'); |
| 358 |
|
| 359 |
char lastChar = graphicCmdBuffer.back(); |
| 360 |
bool isAlpha = ('a' <= lastChar && lastChar <= 'z') || |
| 361 |
('A' <= lastChar && lastChar <= 'Z'); |
| 362 |
if (!isPrintCmd && isAlpha && lastChar != one_of('E', 'e', graphicCmdBuffer.front())) { |
| 363 |
|
| 364 |
char nextCmd = graphicCmdBuffer.back(); |
| 365 |
graphicCmdBuffer.pop_back(); |
| 366 |
executeGraphicCommand(); |
| 367 |
graphicCmdBuffer = nextCmd; |
| 368 |
} |
| 369 |
} |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
void MSXPlotter::executeGraphicCommand() |
| 374 |
{ |
| 375 |
if (graphicCmdBuffer.empty()) return; |
| 376 |
|
| 377 |
printDebug("Plotter: executing graphic command '", graphicCmdBuffer, "'"); |
| 378 |
|
| 379 |
char cmd = graphicCmdBuffer[0]; |
| 380 |
std::string args = graphicCmdBuffer.substr(1); |
| 381 |
|
| 382 |
|
| 383 |
std::vector<float> coords; |
| 384 |
size_t pos = 0; |
| 385 |
while (pos < args.size()) { |
| 386 |
|
| 387 |
while (pos < args.size() && (args[pos] == ',' || args[pos] == ' ')) { |
| 388 |
++pos; |
| 389 |
} |
| 390 |
if (pos >= args.size()) break; |
| 391 |
|
| 392 |
|
| 393 |
size_t nextPos; |
| 394 |
try { |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
char c = args[pos]; |
| 399 |
if (('0' <= c && c <= '9') || c == one_of('-', '+', '.')) { |
| 400 |
|
| 401 |
|
| 402 |
float val = std::stof(args.substr(pos), &nextPos); |
| 403 |
coords.push_back(val); |
| 404 |
pos += nextPos; |
| 405 |
} else { |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
++pos; |
| 410 |
} |
| 411 |
} catch (...) { |
| 412 |
|
| 413 |
++pos; |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
if (cmd != 'D' && cmd != 'J') { |
| 420 |
liftPen(); |
| 421 |
} |
| 422 |
|
| 423 |
switch (cmd) { |
| 424 |
case 'H': |
| 425 |
printDebug("Plotter: H - Home"); |
| 426 |
penPosition = gl::vec2{0.0f}; |
| 427 |
break; |
| 428 |
|
| 429 |
case 'M': |
| 430 |
if (coords.size() >= 2) { |
| 431 |
printDebug("Plotter: M - Move to (", coords[0], ",", coords[1], ")"); |
| 432 |
moveTo({coords[0], coords[1]}); |
| 433 |
} |
| 434 |
break; |
| 435 |
|
| 436 |
case 'R': |
| 437 |
if (coords.size() >= 2) { |
| 438 |
printDebug("Plotter: R - Relative move (", coords[0], ",", coords[1], ")"); |
| 439 |
moveTo(penPosition + gl::vec2{coords[0], coords[1]}); |
| 440 |
} |
| 441 |
break; |
| 442 |
|
| 443 |
case 'D': |
| 444 |
|
| 445 |
printDebug("Plotter: D - Draw absolute, ", coords.size() / 2, " segments, penDown=", penDown); |
| 446 |
setPenDown(true); |
| 447 |
for (size_t i = 0; i + 1 < coords.size(); i += 2) { |
| 448 |
gl::vec2 target{coords[i], coords[i + 1]}; |
| 449 |
lineTo(target); |
| 450 |
} |
| 451 |
setPenDown(false); |
| 452 |
break; |
| 453 |
|
| 454 |
case 'J': |
| 455 |
|
| 456 |
printDebug("Plotter: J - Draw relative, ", coords.size() / 2, " segments, penDown=", penDown); |
| 457 |
setPenDown(true); |
| 458 |
for (size_t i = 0; i + 1 < coords.size(); i += 2) { |
| 459 |
lineTo(penPosition + gl::vec2{coords[i], coords[i + 1]}); |
| 460 |
} |
| 461 |
setPenDown(false); |
| 462 |
break; |
| 463 |
|
| 464 |
case 'S': |
| 465 |
if (!coords.empty()) { |
| 466 |
charScale = std::clamp(int(coords[0]), 0, 15); |
| 467 |
updateLineFeed(); |
| 468 |
printDebug("Plotter: S - Scale set to ", charScale); |
| 469 |
} |
| 470 |
break; |
| 471 |
|
| 472 |
case 'L': |
| 473 |
if (!coords.empty()) { |
| 474 |
lineType = uint8_t(std::clamp(int(coords[0]), 0, 15)); |
| 475 |
dashDistance = 0.0f; |
| 476 |
printDebug("Plotter: L - Line type set to ", lineType); |
| 477 |
} |
| 478 |
break; |
| 479 |
|
| 480 |
case 'Q': |
| 481 |
if (!coords.empty()) { |
| 482 |
|
| 483 |
|
| 484 |
if (pendingCharGap > 0.0f) { |
| 485 |
switch (pendingGapRotation) { |
| 486 |
case 0: penPosition.x -= pendingCharGap; break; |
| 487 |
case 1: penPosition.y += pendingCharGap; break; |
| 488 |
case 2: penPosition.x += pendingCharGap; break; |
| 489 |
case 3: penPosition.y -= pendingCharGap; break; |
| 490 |
} |
| 491 |
printDebug("Plotter: Q - Removed pending gap ", pendingCharGap); |
| 492 |
pendingCharGap = 0.0f; |
| 493 |
} |
| 494 |
rotation = std::clamp(int(coords[0]), 0, 3); |
| 495 |
printDebug("Plotter: Q - Rotation set to ", rotation); |
| 496 |
} |
| 497 |
break; |
| 498 |
|
| 499 |
case 'I': |
| 500 |
|
| 501 |
origin += penPosition; |
| 502 |
penPosition = gl::vec2{0.0f}; |
| 503 |
printDebug("Plotter: I - Origin set to current pos. New Origin=(", origin.x, ",", origin.y, ")"); |
| 504 |
break; |
| 505 |
|
| 506 |
case 'A': |
| 507 |
printDebug("Plotter: A - All Initialize (Reset)"); |
| 508 |
resetSettings(); |
| 509 |
break; |
| 510 |
|
| 511 |
case 'F': { |
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
float drop = (maxLineHeight > 0.0f) ? maxLineHeight : 6.0f * (1.0f + float(charScale)); |
| 520 |
|
| 521 |
|
| 522 |
drop += 2.0f * (1.0f + float(charScale)); |
| 523 |
|
| 524 |
|
| 525 |
switch (rotation) { |
| 526 |
case 0: |
| 527 |
penPosition.y -= drop; |
| 528 |
if (penPosition.y < -1354.0f) { |
| 529 |
flushEmulatedPrinter(); |
| 530 |
ensurePrintPage(); |
| 531 |
penPosition.y = 30.0f; |
| 532 |
} |
| 533 |
penPosition.x = 0.0f; |
| 534 |
break; |
| 535 |
case 1: |
| 536 |
penPosition.x = std::max(penPosition.x - drop, 0.0f); |
| 537 |
penPosition.y = 0.0f; |
| 538 |
break; |
| 539 |
case 2: |
| 540 |
penPosition.y = std::min(penPosition.y + drop, 30.0f); |
| 541 |
penPosition.x = 0.0f; |
| 542 |
break; |
| 543 |
case 3: |
| 544 |
penPosition.x = std::min(penPosition.x + drop, RIGHT_BORDER); |
| 545 |
penPosition.y = 0.0f; |
| 546 |
break; |
| 547 |
} |
| 548 |
|
| 549 |
maxLineHeight = 0.0f; |
| 550 |
printDebug("Plotter: F - New Line, drop=", drop, " rot=", rotation); |
| 551 |
break; |
| 552 |
} |
| 553 |
case 'P': |
| 554 |
if (!args.empty()) { |
| 555 |
|
| 556 |
|
| 557 |
std::string_view text = args; |
| 558 |
printDebug("Plotter: P - Printing '", text, "'"); |
| 559 |
|
| 560 |
bool altChar = false; |
| 561 |
for (auto c : text) { |
| 562 |
if (uint8_t(c) == 0x01) { |
| 563 |
altChar = true; |
| 564 |
} else if (altChar) { |
| 565 |
|
| 566 |
drawCharacter(uint8_t(c) - 64); |
| 567 |
altChar = false; |
| 568 |
} else { |
| 569 |
drawCharacter(uint8_t(c)); |
| 570 |
} |
| 571 |
} |
| 572 |
} |
| 573 |
break; |
| 574 |
|
| 575 |
case 'C': |
| 576 |
if (!coords.empty() && 0 <= coords[0] && coords[0] <= 3) { |
| 577 |
auto newPen = unsigned(coords[0]); |
| 578 |
if (newPen != selectedPen) { |
| 579 |
printDebug("Plotter: C - Select color ", newPen, " (Pen change delay applied)"); |
| 580 |
selectedPen = uint8_t(newPen); |
| 581 |
} |
| 582 |
} |
| 583 |
break; |
| 584 |
|
| 585 |
default: |
| 586 |
|
| 587 |
cliComm.printWarning("Plotter: unknown graphic command '", cmd, "'"); |
| 588 |
break; |
| 589 |
} |
| 590 |
|
| 591 |
if (cmd != 'Q' && cmd != 'P') { |
| 592 |
pendingCharGap = 0.0f; |
| 593 |
} |
| 594 |
graphicCmdBuffer.clear(); |
| 595 |
} |
| 596 |
|
| 597 |
gl::vec2 MSXPlotter::toPaperPos(gl::vec2 plotterPos) const |
| 598 |
{ |
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
gl::vec2 paperPos = gl::vec2{MARGIN.x, FULL_AREA.y - MARGIN.y} |
| 608 |
+ gl::vec2{1.0f, -1.0f} * (plotterPos + origin); |
| 609 |
return paperPos * pixelSize; |
| 610 |
} |
| 611 |
|
| 612 |
float MSXPlotter::penRadius() const |
| 613 |
{ |
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
return pixelSize * ((penThicknessSetting.getEnum() == PenThickness::Thick) ? 1.5f : 1.0f); |
| 619 |
} |
| 620 |
|
| 621 |
gl::vec3 MSXPlotter::penColor() const |
| 622 |
{ |
| 623 |
return inkColors[selectedPen]; |
| 624 |
} |
| 625 |
|
| 626 |
void MSXPlotter::drawDot(gl::vec2 pos) |
| 627 |
{ |
| 628 |
ensurePrintPage(); |
| 629 |
assert(paper); |
| 630 |
paper->draw_dot(toPaperPos(pos), penRadius(), penColor()); |
| 631 |
} |
| 632 |
|
| 633 |
void MSXPlotter::setPenDown(bool newState) |
| 634 |
{ |
| 635 |
if (newState) { |
| 636 |
|
| 637 |
if (!penDown) { |
| 638 |
penDown = true; |
| 639 |
drawDot(penPosition); |
| 640 |
} |
| 641 |
} else { |
| 642 |
liftPen(); |
| 643 |
} |
| 644 |
} |
| 645 |
|
| 646 |
void MSXPlotter::liftPen() |
| 647 |
{ |
| 648 |
penDown = false; |
| 649 |
} |
| 650 |
|
| 651 |
void MSXPlotter::drawLine(gl::vec2 from, gl::vec2 to) |
| 652 |
{ |
| 653 |
assert(penDown); |
| 654 |
|
| 655 |
ensurePrintPage(); |
| 656 |
if (0 < lineType && lineType < 15) { |
| 657 |
auto halfPeriod = float(lineType + 2); |
| 658 |
drawDashedLine(from, to, halfPeriod); |
| 659 |
} else { |
| 660 |
drawSolidLine(from, to); |
| 661 |
dashDistance += length(to - from); |
| 662 |
} |
| 663 |
} |
| 664 |
|
| 665 |
void MSXPlotter::drawDashedLine(gl::vec2 A, gl::vec2 B, float halfPeriod) |
| 666 |
{ |
| 667 |
if (A == B) return; |
| 668 |
|
| 669 |
gl::vec2 AB = B - A; |
| 670 |
float length2 = gl::length2(AB); |
| 671 |
float invLen = gl::rsqrt(length2); |
| 672 |
float len = length2 * invLen; |
| 673 |
|
| 674 |
float period = 2.0f * halfPeriod; |
| 675 |
float phase = std::fmod(dashDistance, period); |
| 676 |
dashDistance += len; |
| 677 |
|
| 678 |
float t_start = (phase < halfPeriod) ? -phase : (period - phase); |
| 679 |
float t_end = t_start + halfPeriod; |
| 680 |
|
| 681 |
|
| 682 |
float period_alpha = period * invLen; |
| 683 |
float alpha_start = t_start * invLen; |
| 684 |
float alpha_end = t_end * invLen; |
| 685 |
|
| 686 |
|
| 687 |
if (alpha_start < 1.0f) { |
| 688 |
drawSolidLine(A + AB * std::max(0.0f, alpha_start), A + AB * std::min(1.0f, alpha_end)); |
| 689 |
alpha_start += period_alpha; |
| 690 |
alpha_end += period_alpha; |
| 691 |
} |
| 692 |
|
| 693 |
|
| 694 |
while (alpha_end < 1.0f) { |
| 695 |
drawSolidLine(A + AB * alpha_start, A + AB * alpha_end); |
| 696 |
alpha_start += period_alpha; |
| 697 |
alpha_end += period_alpha; |
| 698 |
} |
| 699 |
|
| 700 |
|
| 701 |
if (alpha_start < 1.0f) { |
| 702 |
drawSolidLine(A + AB * alpha_start, B); |
| 703 |
} |
| 704 |
} |
| 705 |
|
| 706 |
void MSXPlotter::drawSolidLine(gl::vec2 A, gl::vec2 B) |
| 707 |
{ |
| 708 |
assert(paper); |
| 709 |
paper->draw_motion(toPaperPos(A), toPaperPos(B), penRadius(), penColor()); |
| 710 |
} |
| 711 |
|
| 712 |
void MSXPlotter::ensurePrintPage() |
| 713 |
{ |
| 714 |
if (paper) return; |
| 715 |
|
| 716 |
static constexpr auto MM_PER_INCH = 25.4f; |
| 717 |
auto paperSize = trunc(A4_SIZE / MM_PER_INCH * float(dpiSetting->getInt())); |
| 718 |
pixelSize = float(paperSize.x) / FULL_AREA.x; |
| 719 |
paper = std::make_unique<PlotterPaper>(paperSize); |
| 720 |
} |
| 721 |
|
| 722 |
void MSXPlotter::flushEmulatedPrinter() |
| 723 |
{ |
| 724 |
liftPen(); |
| 725 |
if (!paper) return; |
| 726 |
|
| 727 |
if (!paper->empty()) { |
| 728 |
try { |
| 729 |
static constexpr std::string_view PRINT_DIR = "prints"; |
| 730 |
static constexpr std::string_view PRINT_EXTENSION = ".png"; |
| 731 |
auto filename = FileOperations::getNextNumberedFileName(PRINT_DIR, "page", PRINT_EXTENSION); |
| 732 |
|
| 733 |
auto rgb = paper->getRGB(); |
| 734 |
auto size = rgb.size(); |
| 735 |
small_buffer<const uint8_t*, 4096> rowPointers(std::views::transform(xrange(size.y), |
| 736 |
[&](int y) { return &rgb.getLine(y).data()->x; })); |
| 737 |
|
| 738 |
PNG::saveRGB(size.x, rowPointers, filename); |
| 739 |
|
| 740 |
cliComm.printInfo("Printed to ", filename); |
| 741 |
} catch (MSXException& e) { |
| 742 |
cliComm.printWarning("Failed to print: ", e.getMessage()); |
| 743 |
} |
| 744 |
} |
| 745 |
paper.reset(); |
| 746 |
} |
| 747 |
|
| 748 |
void MSXPlotter::resetSettings() |
| 749 |
{ |
| 750 |
mode = Mode::TEXT; |
| 751 |
escState = EscState::NONE; |
| 752 |
selectedPen = 0; |
| 753 |
penDown = false; |
| 754 |
penPosition = gl::vec2{0.0f, 30.0f}; |
| 755 |
origin = gl::vec2{0.0f, 1354.0f}; |
| 756 |
lineType = 0; |
| 757 |
dashDistance = 0.0f; |
| 758 |
rotation = 0; |
| 759 |
pendingCharGap = 0.0f; |
| 760 |
pendingGapRotation = 0; |
| 761 |
charScale = 1; |
| 762 |
maxLineHeight = 0.0f; |
| 763 |
graphicCmdBuffer.clear(); |
| 764 |
printNext = false; |
| 765 |
terminatorSkip = TerminatorSkip::NONE; |
| 766 |
|
| 767 |
|
| 768 |
updateLineFeed(); |
| 769 |
} |
| 770 |
|
| 771 |
void MSXPlotter::moveTo(gl::vec2 pos) |
| 772 |
{ |
| 773 |
penPosition = pos; |
| 774 |
} |
| 775 |
|
| 776 |
void MSXPlotter::lineTo(gl::vec2 pos) |
| 777 |
{ |
| 778 |
drawLine(penPosition, pos); |
| 779 |
penPosition = pos; |
| 780 |
} |
| 781 |
|
| 782 |
void MSXPlotter::drawCharacter(uint8_t c) |
| 783 |
{ |
| 784 |
ScopedAssign savedLineType(lineType, 0); |
| 785 |
ScopedAssign savedDashDistance(dashDistance, dashDistance); |
| 786 |
|
| 787 |
|
| 788 |
ScopedAssign savedPenState(penDown, true); |
| 789 |
|
| 790 |
auto fontVariant = [&] { |
| 791 |
switch (getCharacterSet()) { |
| 792 |
using enum CharacterSet; |
| 793 |
default: |
| 794 |
case International: return PlotterFontVariant::International; |
| 795 |
case DIN: return PlotterFontVariant::DIN; |
| 796 |
} |
| 797 |
}(); |
| 798 |
auto glyph = decodePRNC41Glyph(fontVariant, c); |
| 799 |
|
| 800 |
float scaleFactor = 1.0f + float(charScale); |
| 801 |
float unitX = 0.8f * scaleFactor; |
| 802 |
float unitY = 1.0f * scaleFactor; |
| 803 |
|
| 804 |
maxLineHeight = std::max(maxLineHeight, 6.0f * unitY); |
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
auto transform = [&](gl::vec2 p) -> gl::vec2 { |
| 811 |
|
| 812 |
static constexpr std::array<gl::mat2, 4> rotations = {{ |
| 813 |
gl::mat2{gl::vec2{ 1.0f, 0.0f}, gl::vec2{ 0.0f, 1.0f}}, |
| 814 |
gl::mat2{gl::vec2{ 0.0f, -1.0f}, gl::vec2{ 1.0f, 0.0f}}, |
| 815 |
gl::mat2{gl::vec2{-1.0f, 0.0f}, gl::vec2{ 0.0f, -1.0f}}, |
| 816 |
gl::mat2{gl::vec2{ 0.0f, 1.0f}, gl::vec2{-1.0f, 0.0f}}, |
| 817 |
}}; |
| 818 |
return penPosition + rotations[rotation] * p; |
| 819 |
}; |
| 820 |
|
| 821 |
gl::vec2 glyphPos{0.0f, 0.0f}; |
| 822 |
for (const auto& command : glyph) { |
| 823 |
gl::vec2 nextGlyphPos = glyphPos + gl::vec2{ |
| 824 |
command.delta.x * unitX, |
| 825 |
command.delta.y * unitY, |
| 826 |
}; |
| 827 |
if (command.type == PlotterFontCommandType::Draw) { |
| 828 |
drawLine(transform(glyphPos), transform(nextGlyphPos)); |
| 829 |
} |
| 830 |
glyphPos = nextGlyphPos; |
| 831 |
} |
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
float charWidthOnly = 5.0f * unitX; |
| 836 |
float charGap = 2.0f * scaleFactor; |
| 837 |
float charAdvance = charWidthOnly + charGap; |
| 838 |
|
| 839 |
penPosition = transform({charAdvance, 0.0f}); |
| 840 |
|
| 841 |
|
| 842 |
pendingCharGap = charGap; |
| 843 |
pendingGapRotation = rotation; |
| 844 |
|
| 845 |
printDebug("Plotter: Rotation ", rotation, |
| 846 |
" | charWidth ", charWidthOnly, |
| 847 |
" | charGap ", charGap, |
| 848 |
" | charAdvance ", charAdvance, |
| 849 |
" | plotter.x ", penPosition.x, |
| 850 |
" | plotter.y ", penPosition.y); |
| 851 |
} |
| 852 |
|
| 853 |
void MSXPlotter::updateLineFeed() |
| 854 |
{ |
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
lineFeed = 9.0f * (1.0f + float(charScale)); |
| 860 |
} |
| 861 |
|
| 862 |
|
| 863 |
INSTANTIATE_SERIALIZE_METHODS(MSXPlotter); |
| 864 |
REGISTER_POLYMORPHIC_INITIALIZER(Pluggable, MSXPlotter, "MSXPlotter"); |
| 865 |
|
| 866 |
} |