|
12 | 12 | #include "src/util/Utils.h" |
13 | 13 | #include "src/Application.h" |
14 | 14 | #include <CEGUI/CEGUI.h> |
| 15 | +#include <CEGUI/FreeTypeFont.h> |
15 | 16 | #include <CEGUI/RendererModules/OpenGL/GLRenderer.h> |
16 | 17 | #include <CEGUI/RendererModules/OpenGL/GL3Renderer.h> |
17 | 18 | #include <CEGUI/RendererModules/OpenGL/ViewportTarget.h> |
|
25 | 26 | #include "qopenglframebufferobject.h" |
26 | 27 | #include "qopenglfunctions.h" |
27 | 28 | #include <qopenglfunctions_3_2_core.h> |
| 29 | +#include <qdom.h> |
| 30 | +#include <qtextstream.h> |
28 | 31 |
|
29 | 32 | // Allows us to register subscribers that want CEGUI log info |
30 | 33 | // This prevents writing CEGUI.log into CWD and allow log display inside the app |
@@ -648,6 +651,82 @@ QStringList CEGUIManager::getAvailableFonts() const |
648 | 651 | return fonts; |
649 | 652 | } |
650 | 653 |
|
| 654 | +bool CEGUIManager::saveFont(CEGUI::Font& font, bool addToSchemes) const |
| 655 | +{ |
| 656 | + const QString fontName = CEGUIUtils::stringToQString(font.getName()); |
| 657 | + const QString fontDescFileName = fontName + ".font"; |
| 658 | + |
| 659 | + // Save an XML font description to the project |
| 660 | + // TODO: rewrite with FontManager::writeFontToStream! |
| 661 | + |
| 662 | + QDomDocument doc; |
| 663 | + auto xmlHeader = doc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"utf-8\""); |
| 664 | + doc.appendChild(xmlHeader); |
| 665 | + auto xmlRoot = doc.createElement("Font"); |
| 666 | + xmlRoot.setAttribute("version", "3"); |
| 667 | + xmlRoot.setAttribute("name", fontName); |
| 668 | + xmlRoot.setAttribute("filename", CEGUIUtils::stringToQString(font.getFileName())); |
| 669 | + xmlRoot.setAttribute("type", CEGUIUtils::stringToQString(font.getTypeName())); |
| 670 | + if (auto freeTypeFont = dynamic_cast<CEGUI::FreeTypeFont*>(&font)) |
| 671 | + xmlRoot.setAttribute("size", QString::number(static_cast<int>(freeTypeFont->getSize()))); |
| 672 | + xmlRoot.setAttribute("nativeHorzRes", QString::number(static_cast<int>(font.getNativeResolution().d_width))); |
| 673 | + xmlRoot.setAttribute("nativeVertRes", QString::number(static_cast<int>(font.getNativeResolution().d_height))); |
| 674 | + xmlRoot.setAttribute("autoScaled", CEGUIUtils::stringToQString(CEGUI::PropertyHelper<CEGUI::AutoScaledMode>::toString(font.getAutoScaled()))); |
| 675 | + doc.appendChild(xmlRoot); |
| 676 | + |
| 677 | + { |
| 678 | + const auto dstFontDescPath = currentProject->getResourceFilePath(fontDescFileName, "fonts"); |
| 679 | + QFile file(dstFontDescPath); |
| 680 | + if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) return false; |
| 681 | + |
| 682 | + QTextStream stream(&file); |
| 683 | + doc.save(stream, 4); |
| 684 | + file.close(); |
| 685 | + } |
| 686 | + |
| 687 | + // Add a new font to all schemes |
| 688 | + |
| 689 | + if (!addToSchemes) return true; |
| 690 | + |
| 691 | + const auto absoluteSchemesPath = currentProject->getAbsolutePathOf(currentProject->schemesPath); |
| 692 | + if (!QDir(absoluteSchemesPath).exists()) return true; |
| 693 | + |
| 694 | + QDirIterator schemesIt(absoluteSchemesPath); |
| 695 | + while (schemesIt.hasNext()) |
| 696 | + { |
| 697 | + schemesIt.next(); |
| 698 | + QFileInfo info = schemesIt.fileInfo(); |
| 699 | + if (info.isDir() || info.suffix() != "scheme") continue; |
| 700 | + |
| 701 | + // Open, read & close file. We will work with a DOM document. |
| 702 | + { |
| 703 | + QFile file(info.absoluteFilePath()); |
| 704 | + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) |
| 705 | + return false; |
| 706 | + |
| 707 | + if (!doc.setContent(&file)) |
| 708 | + return false; |
| 709 | + } |
| 710 | + |
| 711 | + auto xmlGUIScheme = doc.documentElement(); |
| 712 | + QDomElement xmlFontRec = doc.createElement("Font"); |
| 713 | + xmlFontRec.setAttribute("filename", fontDescFileName); |
| 714 | + xmlGUIScheme.appendChild(xmlFontRec); |
| 715 | + |
| 716 | + // Write XML back with changes |
| 717 | + { |
| 718 | + QFile file(info.absoluteFilePath()); |
| 719 | + if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) return false; |
| 720 | + |
| 721 | + QTextStream stream(&file); |
| 722 | + doc.save(stream, 4); |
| 723 | + file.close(); |
| 724 | + } |
| 725 | + } |
| 726 | + |
| 727 | + return true; |
| 728 | +} |
| 729 | + |
651 | 730 | // Retrieves names of images that are available from the set of schemes that were loaded. |
652 | 731 | // see syncProjectToCEGUIInstance |
653 | 732 | QStringList CEGUIManager::getAvailableImages() const |
|
0 commit comments