From 34c0a94f98dc9ff6b0e852e47fc4b8fc6b22f22b Mon Sep 17 00:00:00 2001 From: mniip Date: Tue, 10 Mar 2020 02:00:13 +0300 Subject: [PATCH] Add a font.cpp merging utility --- src/PowderToySDL.cpp | 21 ++++++++++++--- src/gui/font/FontEditor.cpp | 53 ++++++++++++++++++++++++++++++------- src/gui/font/FontEditor.h | 9 ++++--- 3 files changed, 65 insertions(+), 18 deletions(-) diff --git a/src/PowderToySDL.cpp b/src/PowderToySDL.cpp index 7f11a411b..9807e4aa2 100644 --- a/src/PowderToySDL.cpp +++ b/src/PowderToySDL.cpp @@ -873,13 +873,26 @@ int main(int argc, char * argv[]) #else // FONTEDITOR if(argc <= 1) - throw std::runtime_error("Usage: " + ByteString(argv[0]) + " ./data/font.cpp"); - engine->ShowWindow(new FontEditor(argv[1])); + throw std::runtime_error("Usage: \n" + " Edit the font:\n" + " " + ByteString(argv[0]) + " ./data/font.cpp\n" + " Copy characters from source to target:\n" + " " + ByteString(argv[0]) + " \n"); + if(argc <= 2) + { + engine->ShowWindow(new FontEditor(argv[1])); + EngineProcess(); + SaveWindowPosition(); + } + else + { + FontEditor(argv[1], argv[2]); + } #endif - +#ifndef FONTEDITOR EngineProcess(); - SaveWindowPosition(); +#endif #if !defined(DEBUG) && !defined(_DEBUG) } diff --git a/src/gui/font/FontEditor.cpp b/src/gui/font/FontEditor.cpp index 150f99077..09288dccf 100644 --- a/src/gui/font/FontEditor.cpp +++ b/src/gui/font/FontEditor.cpp @@ -21,12 +21,12 @@ unsigned char *font_data; unsigned short *font_ptrs; unsigned int (*font_ranges)[2]; -void FontEditor::ReadHeader(ByteString header) +void FontEditor::ReadDataFile(ByteString dataFile) { std::fstream file; - file.open(header, std::ios_base::in); + file.open(dataFile, std::ios_base::in); if(!file) - throw std::runtime_error("Could not open " + header); + throw std::runtime_error("Could not open " + dataFile); file >> std::skipws; ByteString word; @@ -122,12 +122,12 @@ void FontEditor::ReadHeader(ByteString header) file.close(); } -void FontEditor::WriteHeader(ByteString header, std::vector const &fontData, std::vector const &fontPtrs, std::vector > const &fontRanges) +void FontEditor::WriteDataFile(ByteString dataFile, std::vector const &fontData, std::vector const &fontPtrs, std::vector > const &fontRanges) { std::fstream file; - file.open(header, std::ios_base::out | std::ios_base::trunc); + file.open(dataFile, std::ios_base::out | std::ios_base::trunc); if(!file) - throw std::runtime_error("Could not open " + header); + throw std::runtime_error("Could not open " + dataFile); file << std::setfill('0') << std::hex << std::uppercase; file << beforeFontData << std::endl; @@ -276,15 +276,15 @@ public: }; #define FONT_SCALE 16 -FontEditor::FontEditor(ByteString _header): +FontEditor::FontEditor(ByteString _dataFile): ui::Window(ui::Point(0, 0), ui::Point(WINDOWW, WINDOWH)), - header(_header), + dataFile(_dataFile), currentChar(0x80), fgR(255), fgG(255), fgB(255), bgR(0), bgG(0), bgB(0), grid(1), rulers(1) { - ReadHeader(header); + ReadDataFile(dataFile); UnpackData(fontWidths, fontPixels, fontData, fontPtrs, fontRanges); font_data = fontData.data(); font_ptrs = fontPtrs.data(); @@ -463,6 +463,39 @@ FontEditor::FontEditor(ByteString _header): AddComponent(inputPreview); } +FontEditor::FontEditor(ByteString target, ByteString source): + ui::Window(ui::Point(0, 0), ui::Point(WINDOWW, WINDOWH)) +{ + ReadDataFile(target); + std::map tgtFontWidths, srcFontWidths; + std::map, FONT_H> > tgtFontPixels, srcFontPixels; + UnpackData(tgtFontWidths, tgtFontPixels, fontData, fontPtrs, fontRanges); + ReadDataFile(source); + UnpackData(srcFontWidths, srcFontPixels, fontData, fontPtrs, fontRanges); + for(auto const &p : srcFontPixels) + if(tgtFontPixels.count(p.first)) + { + bool same = tgtFontWidths[p.first] == srcFontWidths[p.first]; + if(same) + for(int j = 0; j < FONT_H; j++) + for(int i = 0; i < tgtFontWidths[p.first]; i++) + same = same && tgtFontPixels[p.first][j][i] == srcFontPixels[p.first][j][i]; + if(!same) + std::cout << "U+" << std::hex << p.first << " is present in both files and is different!" << std::endl; + } + else + { + std::cout << "Adding U+" << std::hex << p.first << " to the target" << std::endl; + tgtFontWidths[p.first] = srcFontWidths[p.first]; + tgtFontPixels[p.first] = p.second; + } + std::vector tmpFontData; + std::vector tmpFontPtrs; + std::vector > tmpFontRanges; + PackData(tgtFontWidths, tgtFontPixels, tmpFontData, tmpFontPtrs, tmpFontRanges); + WriteDataFile(target, tmpFontData, tmpFontPtrs, tmpFontRanges); +} + void FontEditor::OnDraw() { Graphics *g = GetGraphics(); @@ -622,7 +655,7 @@ void FontEditor::Save() std::vector tmpFontPtrs; std::vector > tmpFontRanges; PackData(fontWidths, fontPixels, tmpFontData, tmpFontPtrs, tmpFontRanges); - WriteHeader(header, tmpFontData, tmpFontPtrs, tmpFontRanges); + WriteDataFile(dataFile, tmpFontData, tmpFontPtrs, tmpFontRanges); savedButton->SetToggleState(true); } #endif diff --git a/src/gui/font/FontEditor.h b/src/gui/font/FontEditor.h index 310dea5c9..25c2f759e 100644 --- a/src/gui/font/FontEditor.h +++ b/src/gui/font/FontEditor.h @@ -20,7 +20,7 @@ namespace ui class FontEditor: public ui::Window { private: - ByteString header; + ByteString dataFile; std::map fontWidths; std::map, FONT_H> > fontPixels; @@ -33,8 +33,8 @@ private: ByteString afterFontPtrs; ByteString afterFontRanges; - void ReadHeader(ByteString header); - void WriteHeader(ByteString header, std::vector const &fontData, std::vector const &fontPtrs, std::vector > const &fontRanges); + void ReadDataFile(ByteString dataFile); + void WriteDataFile(ByteString dataFile, std::vector const &fontData, std::vector const &fontPtrs, std::vector > const &fontRanges); static void PackData( std::map const &fontWidths, std::map, FONT_H> > const &fontPixels, @@ -71,7 +71,8 @@ private: void Translate(std::array, FONT_H> &, int dx, int dy); public: - FontEditor(ByteString header); + FontEditor(ByteString dataFile); + FontEditor(ByteString target, ByteString source); /* Merge mode */ void OnDraw() override; void OnMouseDown(int x, int y, unsigned button) override;