Vuo  2.3.2
VuoRendererFonts.cc
Go to the documentation of this file.
1 
10 #include "VuoRendererFonts.hh"
11 
12 VuoRendererFonts *VuoRendererFonts::sharedFonts = NULL;
13 
14 const qreal VuoRendererFonts::thickPenWidth = 20;
16 
17 const QString VuoRendererFonts::fontFamily = "PT Sans";
18 
23 
30 {
31  if (! sharedFonts)
32  sharedFonts = new VuoRendererFonts();
33 
34  return sharedFonts;
35 }
36 
42 VuoRendererFonts::VuoRendererFonts(void)
43 {
44  addFont("PTS55F.ttf");
45  addFont("PTS75F.ttf");
46 }
47 
51 void VuoRendererFonts::addFont(QString font)
52 {
53  // Try loading from the Editor app bundle
54  {
55  QString fontPath = QDir(QApplication::applicationDirPath().append("/../Resources/").append(font)).canonicalPath();
56  if (fontPath.count() && QFontDatabase::addApplicationFont(fontPath) != -1)
57  return;
58  }
59 
60  // Try loading from the SDK
61  {
62  QString fontPath = QDir(QApplication::applicationDirPath().append("/resources/").append(font)).canonicalPath();
63  if (fontPath.count() && QFontDatabase::addApplicationFont(fontPath) != -1)
64  return;
65  }
66 
67  // Try loading from the source tree (e.g., when running tests).
68  {
69  QString fontPath = QDir(QApplication::applicationDirPath().append("/../../renderer/font/").append(font)).canonicalPath();
70  if (fontPath.count() && QFontDatabase::addApplicationFont(fontPath) != -1)
71  return;
72  }
73 
74  VUserLog("Error: Failed to open '%s'.", font.toUtf8().data());
75 }
76 
81 {
82  return QFont(fontFamily, nodeTitleFontSize, QFont::Bold, false);
83 }
84 
89 {
90  return QFont(fontFamily, nodeClassFontSize, QFont::Light, false);
91 }
92 
97 {
98  return QFont(fontFamily, portTitleFontSize, QFont::Light, false);
99 }
100 
105 {
106  return QFont(fontFamily, portDetailFontSize, QFont::Light, false);
107 }
108 
113 {
114  return QFont(fontFamily, portTitleFontSize, QFont::Light, false);
115 }
116 
121 {
122  return QFont(fontFamily, 14, QFont::Bold, false);
123 }
124 
129 {
130  return QFont(fontFamily, 12, QFont::Light, false);
131 }
132 
137 {
138  return QFont(fontFamily, 14, QFont::Light, false);
139 }
140 
145 double VuoRendererFonts::getHorizontalOffset(const QFont font, const QString text)
146 {
147  if (text.length() == 0)
148  return 0;
149 
150  QPainterPath p;
151  p.addText(0, 0, font, text[0]);
152  return p.boundingRect().x();
153 }
154 
158 QString VuoRendererFonts::getCSS(QFont font)
159 {
160  int qtweight = font.weight();
161  int cssweight = 400; // "normal"
162  if (qtweight < 40)
163  cssweight = 300; // "light"
164  else if (qtweight > 60)
165  cssweight = 700; // "bold"
166 
167  return QString("font-family: %1; font-size: %2pt; font-weight: %3;")
168  .arg(font.family())
169  .arg(font.pointSizeF())
170  .arg(cssweight);
171 }