Skip to content

Commit

Permalink
system fonts
Browse files Browse the repository at this point in the history
  • Loading branch information
JC3 committed Jul 7, 2023
1 parent 8cd29c3 commit 2de18af
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 32 deletions.
52 changes: 52 additions & 0 deletions circuits.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "circuits.h"
#include <QFont>
#include <QPainter>
#include <QDebug>
#include <stdexcept>

Expand Down Expand Up @@ -194,4 +196,54 @@ Blueprint * Text (QImage font, QString fontCharset, int kerning, QString text, B
}


Blueprint * Text (QFont font, int fontHeight, QString text, Blueprint::Ink logicInk, Blueprint::Ink decoOnInk, Blueprint::Ink decoOffInk) {

// === generate font image ===

font.setPixelSize(fontHeight);
font.setStyleStrategy(QFont::NoAntialias);
font.setHintingPreference(QFont::PreferFullHinting);

QRect rcText = QFontMetrics(font).boundingRect(text);
QImage textImage(rcText.size(), QImage::Format_RGB888);

{
QPainter p(&textImage);
p.setRenderHint(QPainter::Antialiasing, false);
p.setRenderHint(QPainter::TextAntialiasing, false);
p.fillRect(textImage.rect(), Qt::black);
p.setPen(Qt::white);
p.setFont(font);
p.drawText(textImage.rect(), Qt::AlignHCenter | Qt::AlignVCenter | Qt::TextSingleLine, text);
}

//textImage.save("debug-fontimage.png");

// === now copy it to every layer ===

QVector<QPair<Blueprint::Layer,Blueprint::Ink> > layerInks;
layerInks.append({Blueprint::Logic, logicInk});
layerInks.append({Blueprint::DecoOn, decoOnInk});
layerInks.append({Blueprint::DecoOff, decoOffInk});

Blueprint *bp = new Blueprint(textImage.width(), textImage.height());

for (auto layerInk : layerInks) {
const Blueprint::Layer layer = layerInk.first;
const Blueprint::Ink ink = layerInk.second;
if (!ink.isValid())
continue;
for (int y = 0; y < textImage.height(); ++ y) {
for (int x = 0; x < textImage.width(); ++ x) {
QRgb srcpix = textImage.pixel(x, y);
if (qRed(srcpix) > 128) bp->setPixel(layer, x, y, ink);
}
}
}

return bp;

}


}
1 change: 1 addition & 0 deletions circuits.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ enum ROMAddress0Side { Near=0, Far=1 };

Blueprint * ROM (int addressBits, int dataBits, ROMDataLSBSide dataLSB, ROMAddress0Side addr0Side, const QVector<quint64> &data, bool omitEmpty);
Blueprint * Text (QImage font, QString fontCharset, int kerning, QString text, Blueprint::Ink logicInk = Blueprint::Annotation, Blueprint::Ink decoOnInk = Blueprint::Invalid, Blueprint::Ink decoOffInk = Blueprint::Invalid);
Blueprint * Text (QFont font, int fontHeight, QString text, Blueprint::Ink logicInk, Blueprint::Ink decoOnInk, Blueprint::Ink decoOffInk);

}

Expand Down
58 changes: 48 additions & 10 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,15 +417,6 @@ void MainWindow::doGenerateText () {
{ 2, Blueprint::LED }
};

QString fontfile = fonts_[ui_->cbTextFont->currentText()].filename;
if (fontfile == "")
throw runtime_error("invalid internal font id");
QImage fontimage;
if (!fontimage.load(fontfile))
throw runtime_error(("couldn't load " + fontfile).toStdString());
QString charset = fonts_[ui_->cbTextFont->currentText()].charset;
int kerning = fonts_[ui_->cbTextFont->currentText()].kerning;

QString text = ui_->txtTextContent->text();
Blueprint::Ink logicInk, onInk, offInk;
if (ui_->chkTextLogic->isChecked())
Expand All @@ -435,7 +426,30 @@ void MainWindow::doGenerateText () {
if (ui_->chkTextDecoOff->isChecked())
offInk = ui_->clrTextDecoOff->selectedColor();

Blueprint *bp = Circuits::Text(fontimage, charset, kerning, text, logicInk, onInk, offInk);
Blueprint *bp;

if (ui_->btnFontBuiltIn->isChecked()) {

QString fontfile = fonts_[ui_->cbTextFont->currentText()].filename;
if (fontfile == "")
throw runtime_error("invalid internal font id");
QImage fontimage;
if (!fontimage.load(fontfile))
throw runtime_error(("couldn't load " + fontfile).toStdString());
QString charset = fonts_[ui_->cbTextFont->currentText()].charset;
int kerning = fonts_[ui_->cbTextFont->currentText()].kerning;

bp = Circuits::Text(fontimage, charset, kerning, text, logicInk, onInk, offInk);

} else {

QFont font = ui_->cbSystemFont->currentFont();
int height = ui_->spnSystemFontHeight->value();

bp = Circuits::Text(font, height, text, logicInk, onInk, offInk);

}

ui_->txtTextBP->setPlainText(bp->bpString());

if (ui_->chkTextAutoCopy->isChecked())
Expand Down Expand Up @@ -524,3 +538,27 @@ void MainWindow::on_btnMiscX11_clicked()
ui_->txtMisc->setPlainText(palette);
}


void MainWindow::on_cbSystemFont_activated(int index)
{
doGenerateText();
}


void MainWindow::on_spnSystemFontHeight_valueChanged(int arg1)
{
doGenerateText();
}


void MainWindow::on_btnFontSystem_toggled(bool checked)
{
doGenerateText();
}


void MainWindow::on_btnFontBuiltIn_toggled(bool checked)
{
doGenerateText();
}

8 changes: 8 additions & 0 deletions mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ private slots:

void on_btnConvertEmoji_clicked();

void on_cbSystemFont_activated(int index);

void on_spnSystemFontHeight_valueChanged(int arg1);

void on_btnFontSystem_toggled(bool checked);

void on_btnFontBuiltIn_toggled(bool checked);

private:

struct FontDesc {
Expand Down
114 changes: 93 additions & 21 deletions mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<item row="1" column="0">
<widget class="QTabWidget" name="tabWidget">
<property name="currentIndex">
<number>0</number>
<number>2</number>
</property>
<widget class="QWidget" name="tabConvert">
<attribute name="title">
Expand Down Expand Up @@ -433,7 +433,14 @@
<string>Generate Text</string>
</attribute>
<layout class="QGridLayout" name="gridLayout_5">
<item row="2" column="0" colspan="2">
<item row="6" column="0" colspan="2">
<widget class="QPlainTextEdit" name="txtTextBP">
<property name="placeholderText">
<string>Blueprint</string>
</property>
</widget>
</item>
<item row="5" column="0" colspan="2">
<layout class="QGridLayout" name="gridLayout_6">
<property name="rightMargin">
<number>7</number>
Expand Down Expand Up @@ -605,27 +612,17 @@
</item>
</layout>
</item>
<item row="0" column="0">
<widget class="QComboBox" name="cbTextFont">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item row="1" column="0" colspan="2">
<item row="4" column="0" colspan="2">
<widget class="QCheckBox" name="chkTextAutoCopy">
<property name="text">
<string>Auto copy to clipboard as you type</string>
</property>
</widget>
</item>
<item row="0" column="1">
<item row="0" column="0" colspan="2">
<widget class="QLineEdit" name="txtTextContent">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
Expand All @@ -635,12 +632,86 @@
</property>
</widget>
</item>
<item row="3" column="0" colspan="2">
<widget class="QPlainTextEdit" name="txtTextBP">
<property name="placeholderText">
<string>Blueprint</string>
</property>
</widget>
<item row="1" column="0" colspan="2">
<layout class="QHBoxLayout" name="horizontalLayout_6">
<item>
<widget class="QRadioButton" name="btnFontBuiltIn">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Built In Font</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
<attribute name="buttonGroup">
<string notr="true">grpFontChoice</string>
</attribute>
</widget>
</item>
<item>
<widget class="QComboBox" name="cbTextFont">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="btnFontSystem">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>System Font</string>
</property>
<attribute name="buttonGroup">
<string notr="true">grpFontChoice</string>
</attribute>
</widget>
</item>
<item>
<widget class="QFontComboBox" name="cbSystemFont">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item>
<widget class="QSpinBox" name="spnSystemFontHeight">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="toolTip">
<string>system font height</string>
</property>
<property name="minimum">
<number>1</number>
</property>
<property name="maximum">
<number>1000</number>
</property>
<property name="value">
<number>12</number>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
Expand Down Expand Up @@ -962,5 +1033,6 @@
<connections/>
<buttongroups>
<buttongroup name="grpConvertLayer"/>
<buttongroup name="grpFontChoice"/>
</buttongroups>
</ui>
2 changes: 1 addition & 1 deletion vcbtool.pro
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
QMAKE_TARGET_DESCRIPTION = "VCB Tool"
VERSION = 1.7.2
VERSION = 1.8.0
DEFINES += VCBTOOL_VERSION='\\"$$VERSION\\"'

QT += core gui
Expand Down

0 comments on commit 2de18af

Please sign in to comment.