/ de-omarchy / shell / Ui / OpticalGlyph.qml
swadhinbiswas Render PUA icon glyphs via per-codepoint fontconfig family probe · 8/24/2026
fbcf86e
71 lines · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import QtQuick
import qs.Commons

Item {
  id: root

  property string text: ""
  property string fontFamily: Style.font.family
  property real fontSize: Style.font.body
  property color color: Color.foreground
  property bool debugBounds: false

  // Icon codepoints (Nerd Font / FontAwesome / Material Symbols — the
  // Private Use Areas) render through Style's per-glyph resolver so a family
  // that actually contains the glyph is used; ordinary text keeps the
  // caller's family.
  readonly property bool iconGlyph: {
    var cp = 0
    if (text && text.length > 0) {
      try { cp = text.codePointAt(0) } catch (e) { cp = 0 }
    }
    return cp >= 0xE000
  }
  readonly property string resolvedFamily: root.iconGlyph
    ? Style.iconFamilyFor(root.text)
    : root.fontFamily

  readonly property int renderedFontSize: Math.max(1, Math.round(fontSize))
  readonly property real tightWidth: Math.max(1, glyphMetrics.tightBoundingRect.width)
  readonly property real horizontalCorrection: glyph.implicitWidth / 2 - (glyphMetrics.tightBoundingRect.x + tightWidth / 2)
  readonly property real paintedCenterX: glyph.x + glyphMetrics.tightBoundingRect.x + tightWidth / 2
  readonly property real baselineY: glyph.y + glyph.baselineOffset

  TextMetrics {
    id: glyphMetrics
    font.family: root.resolvedFamily
    font.pixelSize: root.renderedFontSize
    text: root.text
  }

  Text {
    id: glyph
    // Keep the shared line box and baseline intact. Correcting only the
    // horizontal painted bounds avoids per-glyph vertical drift.
    anchors.centerIn: parent
    anchors.horizontalCenterOffset: root.horizontalCorrection
    text: root.text
    color: root.color
    font.family: root.resolvedFamily
    font.pixelSize: root.renderedFontSize
    renderType: Text.NativeRendering
  }

  Rectangle {
    visible: root.debugBounds
    anchors.fill: parent
    color: "transparent"
    border.width: 1
    border.color: "#4488ff"
  }

  Rectangle {
    visible: root.debugBounds
    x: 0
    y: Math.round(root.baselineY)
    width: parent.width
    height: 1
    color: "#44ff88"
  }
}