-
Notifications
You must be signed in to change notification settings - Fork 428
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sanitize font sizes in internal editor
Signed-off-by: Lukas Holecek <hluk@email.cz>
- Loading branch information
Showing
5 changed files
with
113 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
Copyright (c) 2019, Lukas Holecek <hluk@email.cz> | ||
This file is part of CopyQ. | ||
CopyQ is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
CopyQ is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with CopyQ. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include "sanitize_text_document.h" | ||
|
||
#include <QTextBlock> | ||
#include <QTextCursor> | ||
#include <QTextDocument> | ||
|
||
#ifdef Q_OS_MAC | ||
# include <QFontDatabase> | ||
#endif | ||
|
||
constexpr int maxFontPointSize = 128; | ||
constexpr int maxFontPixelSize = maxFontPointSize * 4 / 3; | ||
|
||
void sanitizeTextDocument(QTextDocument *document) | ||
{ | ||
QTextCursor tc(document); | ||
|
||
#ifdef Q_OS_MAC | ||
// Prevent showing font download dialog on macOS. | ||
QFontDatabase fontDatabase; | ||
for (auto block = document->begin(); block != document->end(); block = block.next()) { | ||
for (auto it = block.begin(); !it.atEnd(); ++it) { | ||
const QTextFragment fragment = it.fragment(); | ||
QTextCharFormat charFormat = fragment.charFormat(); | ||
const QString fontFamily = charFormat.fontFamily(); | ||
if ( !fontDatabase.hasFamily(fontFamily) ) { | ||
tc.setPosition( fragment.position() ); | ||
tc.setPosition( fragment.position() + fragment.length(), QTextCursor::KeepAnchor ); | ||
charFormat.setFontFamily(QString()); | ||
tc.setCharFormat(charFormat); | ||
} | ||
} | ||
} | ||
#endif | ||
|
||
// Sanitize font sizes to prevent app freeze. | ||
for (auto block = document->begin(); block != document->end(); block = block.next()) { | ||
for (auto it = block.begin(); !it.atEnd(); ++it) { | ||
const QTextFragment fragment = it.fragment(); | ||
QTextCharFormat charFormat = fragment.charFormat(); | ||
QFont font = charFormat.font(); | ||
|
||
const int pixelSize = font.pixelSize(); | ||
const int pointSize = font.pointSize(); | ||
if (-maxFontPixelSize > pixelSize || pixelSize > maxFontPixelSize) | ||
font.setPixelSize(maxFontPixelSize); | ||
else if (-maxFontPointSize > pointSize || pointSize > maxFontPointSize) | ||
font.setPointSize(maxFontPointSize); | ||
else | ||
continue; | ||
|
||
tc.setPosition( fragment.position() ); | ||
tc.setPosition( fragment.position() + fragment.length(), QTextCursor::KeepAnchor ); | ||
charFormat.setFont(font); | ||
tc.setCharFormat(charFormat); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
Copyright (c) 2019, Lukas Holecek <hluk@email.cz> | ||
This file is part of CopyQ. | ||
CopyQ is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
CopyQ is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with CopyQ. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#ifndef SANITIZE_TEXT_DOCUMENT_H | ||
#define SANITIZE_TEXT_DOCUMENT_H | ||
|
||
class QTextDocument; | ||
|
||
void sanitizeTextDocument(QTextDocument *document); | ||
|
||
#endif // SANITIZE_TEXT_DOCUMENT_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters