-
Notifications
You must be signed in to change notification settings - Fork 76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add QFontMetrics support #898
Open
Montel
wants to merge
10
commits into
KDAB:main
Choose a base branch
from
Montel:add_qfontmetrics
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9d06532
Add QFontMetrics support
Montel 8eb2385
Add constructor from QFont
Montel 781bd5b
Add horizontalAdvance method
Montel 0fa3bbf
Add qfontmetrics test
Montel 3bee54a
Fix coding style
Montel 4c0ffc1
Clean up code
Montel 89a3122
Merge branch 'main' into add_qfontmetrics
Montel f256477
Add test construct qfontmetrics from qfont
Montel 7848e66
Fix type
Montel b4be0cb
We need QGuiApplication before accessing to Font Database
Montel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// clang-format off | ||
// SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]> | ||
// clang-format on | ||
// SPDX-FileContributor: Laurent Montel <[email protected]> | ||
// | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
#pragma once | ||
|
||
#include <QtGui/QFontMetrics> | ||
|
||
#include "rust/cxx.h" | ||
|
||
// Define namespace otherwise we hit a GCC bug | ||
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56480 | ||
namespace rust { | ||
|
||
template<> | ||
struct IsRelocatable<QFontMetrics> : ::std::true_type | ||
{ | ||
}; | ||
|
||
} // namespace rust |
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
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,22 @@ | ||
// clang-format off | ||
// SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]> | ||
// clang-format on | ||
// SPDX-FileContributor: Laurent Montel <[email protected]> | ||
// | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
|
||
#include "cxx-qt-lib/qfontmetrics.h" | ||
|
||
#include "../assertion_utils.h" | ||
|
||
// https://code.qt.io/cgit/qt/qtbase.git/tree/src/gui/text/qfontmetrics.h?h=v5.15.6-lts-lgpl#n147 | ||
// https://code.qt.io/cgit/qt/qtbase.git/tree/src/gui/text/qfontmetrics.h?h=v6.2.4#n117 | ||
assert_alignment_and_size(QFontMetrics, | ||
alignof(::std::size_t), | ||
sizeof(::std::size_t)); | ||
|
||
static_assert(!::std::is_trivially_copy_assignable<QFontMetrics>::value); | ||
static_assert(!::std::is_trivially_copy_constructible<QFontMetrics>::value); | ||
|
||
static_assert(!::std::is_trivially_destructible<QFontMetrics>::value); | ||
static_assert(QTypeInfo<QFontMetrics>::isRelocatable); |
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,146 @@ | ||
// SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]> | ||
// SPDX-FileContributor: Laurent Montel <[email protected]> | ||
// | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
use cxx::{type_id, ExternType}; | ||
use std::mem::MaybeUninit; | ||
|
||
#[cxx::bridge] | ||
mod ffi { | ||
unsafe extern "C++" { | ||
include!("cxx-qt-lib/qfontmetrics.h"); | ||
type QFontMetrics = super::QFontMetrics; | ||
include!("cxx-qt-lib/qrect.h"); | ||
type QRect = crate::QRect; | ||
include!("cxx-qt-lib/qstring.h"); | ||
type QString = crate::QString; | ||
include!("cxx-qt-lib/qfont.h"); | ||
type QFont = crate::QFont; | ||
|
||
/// Returns the ascent of the font. | ||
fn ascent(self: &QFontMetrics) -> i32; | ||
|
||
/// Returns the average width of glyphs in the font. | ||
#[rust_name = "average_char_width"] | ||
fn averageCharWidth(self: &QFontMetrics) -> i32; | ||
|
||
/// Returns the bounding rectangle of the characters in the string specified by text. | ||
/// The bounding rectangle always covers at least the set of pixels the text would cover if drawn at (0, 0). | ||
#[rust_name = "bounding_rect"] | ||
fn boundingRect(self: &QFontMetrics, text: &QString) -> QRect; | ||
|
||
/// Returns the cap height of the font. | ||
/// The cap height of a font is the height of a capital letter above the baseline. It specifically is | ||
/// the height of capital letters that are flat - such as H or I - as opposed to round letters such | ||
/// as O, or pointed letters like A, both of which may display overshoot. | ||
#[rust_name = "cap_height"] | ||
fn capHeight(self: &QFontMetrics) -> i32; | ||
|
||
/// Returns the descent of the font. | ||
/// The descent is the distance from the base line to the lowest point characters extend to. In practice, | ||
/// some font designers break this rule, e.g. to accommodate a certain character, | ||
/// so it is possible (though rare) that this value will be too small. | ||
fn descent(self: &QFontMetrics) -> i32; | ||
|
||
/// Returns the height of the font. | ||
fn height(self: &QFontMetrics) -> i32; | ||
|
||
/// Returns the horizontal advance in pixels of the first len characters of text. If len is negative (the default), | ||
/// the entire string is used. The entire length of text is analysed even if len is substantially shorter. | ||
#[rust_name = "horizontal_advance"] | ||
fn horizontalAdvance(self: &QFontMetrics, text: &QString, len: i32) -> i32; | ||
|
||
/// Returns the leading of the font. | ||
fn leading(self: &QFontMetrics) -> i32; | ||
|
||
/// Returns the distance from one base line to the next. | ||
/// This value is always equal to leading()+height(). | ||
#[rust_name = "line_spacing"] | ||
fn lineSpacing(self: &QFontMetrics) -> i32; | ||
|
||
/// Returns the width of the underline and strikeout lines, adjusted for the point size of the font. | ||
#[rust_name = "line_width"] | ||
fn lineWidth(self: &QFontMetrics) -> i32; | ||
|
||
/// Returns the width of the widest character in the font. | ||
#[rust_name = "max_width"] | ||
fn maxWidth(self: &QFontMetrics) -> i32; | ||
|
||
/// Returns the minimum left bearing of the font. | ||
#[rust_name = "min_left_bearing"] | ||
fn minLeftBearing(self: &QFontMetrics) -> i32; | ||
|
||
/// Returns the minimum right bearing of the font. | ||
#[rust_name = "min_right_bearing"] | ||
fn minRightBearing(self: &QFontMetrics) -> i32; | ||
|
||
/// Returns the distance from the base line to where an overline should be drawn. | ||
#[rust_name = "overline_position"] | ||
fn overlinePos(self: &QFontMetrics) -> i32; | ||
|
||
/// Returns the distance from the base line to where the strikeout line should be drawn. | ||
#[rust_name = "strike_out_position"] | ||
fn strikeOutPos(self: &QFontMetrics) -> i32; | ||
|
||
/// Returns a tight bounding rectangle around the characters in the string specified by text. | ||
/// The bounding rectangle always covers at least the set of pixels the text would cover if drawn at (0, 0). | ||
#[rust_name = "tight_bounding_rect"] | ||
fn tightBoundingRect(self: &QFontMetrics, text: &QString) -> QRect; | ||
|
||
/// Returns the distance from the base line to where an underscore should be drawn. | ||
#[rust_name = "underline_position"] | ||
fn underlinePos(self: &QFontMetrics) -> i32; | ||
|
||
/// Returns the 'x' height of the font. This is often but not always the same as the height of the character 'x'. | ||
#[rust_name = "x_height"] | ||
fn xHeight(self: &QFontMetrics) -> i32; | ||
} | ||
|
||
#[namespace = "rust::cxxqtlib1"] | ||
unsafe extern "C++" { | ||
include!("cxx-qt-lib/common.h"); | ||
|
||
#[doc(hidden)] | ||
#[rust_name = "qfontmetrics_drop"] | ||
fn drop(fontmetics: &mut QFontMetrics); | ||
|
||
#[doc(hidden)] | ||
#[rust_name = "qfontmetrics_clone"] | ||
fn construct(font: &QFontMetrics) -> QFontMetrics; | ||
|
||
#[doc(hidden)] | ||
#[rust_name = "qfontmetrics_init_from_qfont"] | ||
fn construct(font: &QFont) -> QFontMetrics; | ||
} | ||
} | ||
|
||
#[repr(C)] | ||
pub struct QFontMetrics { | ||
_cspec: MaybeUninit<i32>, | ||
Montel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
impl Drop for QFontMetrics { | ||
fn drop(&mut self) { | ||
ffi::qfontmetrics_drop(self); | ||
} | ||
} | ||
|
||
impl Clone for QFontMetrics { | ||
fn clone(&self) -> Self { | ||
ffi::qfontmetrics_clone(self) | ||
} | ||
} | ||
|
||
impl From<&ffi::QFont> for QFontMetrics { | ||
fn from(font: &ffi::QFont) -> Self { | ||
ffi::qfontmetrics_init_from_qfont(font) | ||
} | ||
} | ||
|
||
// Safety: | ||
// | ||
// Static checks on the C++ side to ensure the size is the same. | ||
unsafe impl ExternType for QFontMetrics { | ||
type Id = type_id!("QFontMetrics"); | ||
type Kind = cxx::kind::Trivial; | ||
} |
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
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,40 @@ | ||
// clang-format off | ||
// SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]> | ||
// clang-format on | ||
// SPDX-FileContributor: Laurent Montel <[email protected]> | ||
// | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
#pragma once | ||
|
||
#include <QtGui/QFont> | ||
#include <QtGui/QFontMetrics> | ||
#include <QtTest/QTest> | ||
|
||
#include "cxx-qt-gen/qfontmetrics.cxx.h" | ||
|
||
class QFontMetricsTest : public QObject | ||
{ | ||
Q_OBJECT | ||
|
||
private Q_SLOTS: | ||
void construct() | ||
Montel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
QFont f; | ||
const int pointSize = 40; | ||
f.setPointSize(40); | ||
const auto m = constructor_qfontmetrics(f); | ||
QCOMPARE(m.ascent(), 40); | ||
QCOMPARE(m.height(), 30); | ||
} | ||
void clone() | ||
{ | ||
QFont f; | ||
f.setBold(true); | ||
f.setPointSize(30); | ||
const auto m = QFontMetrics(f); | ||
|
||
const auto c = clone_qfontmetrics(m); | ||
QCOMPARE(m.ascent(), c.ascent()); | ||
QCOMPARE(m.height(), c.height()); | ||
} | ||
}; |
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,30 @@ | ||
// SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]> | ||
// SPDX-FileContributor: Laurent Montel <[email protected]> | ||
// | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
|
||
use cxx_qt_lib::QFont; | ||
use cxx_qt_lib::QFontMetrics; | ||
|
||
#[cxx::bridge] | ||
mod qfontmetrics_cxx { | ||
unsafe extern "C++" { | ||
include!("cxx-qt-lib/qfontmetrics.h"); | ||
|
||
type QFontMetrics = cxx_qt_lib::QFontMetrics; | ||
type QFont = cxx_qt_lib::QFont; | ||
} | ||
|
||
extern "Rust" { | ||
fn clone_qfontmetrics(f: &QFontMetrics) -> QFontMetrics; | ||
fn constructor_qfontmetrics(f: &QFont) -> QFontMetrics; | ||
} | ||
} | ||
|
||
fn clone_qfontmetrics(p: &QFontMetrics) -> QFontMetrics { | ||
p.clone() | ||
} | ||
|
||
fn constructor_qfontmetrics(f: &QFont) -> QFontMetrics { | ||
QFontMetrics::from(f) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a test in the standalones would be good too :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will add it.