Skip to content

Commit 04e0005

Browse files
authored
Add custom font
1 parent 831e9e3 commit 04e0005

File tree

5 files changed

+28
-16
lines changed

5 files changed

+28
-16
lines changed

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,24 @@ import "umplot.um"
88
fn main() {
99
plt := umplot.init(4)
1010
11-
for x := 0.0; x <= 100.0; x += 1.0 {
12-
for i := 0; i < 4; i++ {
11+
for i := 0; i < 4; i++ {
12+
plt.series[i].name = "Sine wave " + repr(i + 1)
13+
14+
for x := 0.0; x <= 100.0; x += 1.0 {
1315
y := (1 + 0.5 * i) * sin(x / 10.0 + i)
1416
plt.series[i].add(x, y)
1517
}
1618
}
1719
1820
plt.series[1].style.kind = umplot.STYLE_SCATTER
1921
22+
plt.titles.graph = "UmPlot demo"
23+
plt.titles.x = "Time (seconds)"
24+
plt.titles.y = "Value"
25+
2026
plt.plot()
2127
}
28+
2229
```
2330
![](umplot.png)
2431

liberation.ttf

108 KB
Binary file not shown.

umplot.c

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ static void drawGraph(const Plot *plot, const ScreenTransform *transform)
246246
}
247247

248248

249-
static void drawGrid(const Plot *plot, const ScreenTransform *transform, int *maxYLabelWidth)
249+
static void drawGrid(const Plot *plot, const ScreenTransform *transform, const Font *font, int *maxYLabelWidth)
250250
{
251251
if (maxYLabelWidth)
252252
*maxYLabelWidth = 0;
@@ -289,7 +289,7 @@ static void drawGrid(const Plot *plot, const ScreenTransform *transform, int *ma
289289
const int labelX = x - labelWidth / 2;
290290
const int labelY = clientRect.y + clientRect.height + plot->grid.fontSize;
291291

292-
DrawText(label, labelX, labelY, plot->grid.fontSize, *(Color *)&plot->grid.color);
292+
DrawTextEx(*font, label, (Vector2){labelX, labelY}, plot->grid.fontSize, 1, *(Color *)&plot->grid.color);
293293
}
294294
}
295295

@@ -309,7 +309,7 @@ static void drawGrid(const Plot *plot, const ScreenTransform *transform, int *ma
309309
const int labelX = clientRect.x - labelWidth - plot->grid.fontSize;
310310
const int labelY = y - plot->grid.fontSize / 2;
311311

312-
DrawText(label, labelX, labelY, plot->grid.fontSize, *(Color *)&plot->grid.color);
312+
DrawTextEx(*font, label, (Vector2){labelX, labelY}, plot->grid.fontSize, 1, *(Color *)&plot->grid.color);
313313

314314
if (maxYLabelWidth && labelWidth > *maxYLabelWidth)
315315
*maxYLabelWidth = labelWidth;
@@ -318,7 +318,7 @@ static void drawGrid(const Plot *plot, const ScreenTransform *transform, int *ma
318318
}
319319

320320

321-
static void drawTitles(const Plot *plot, const ScreenTransform *transform, int maxYLabelWidth)
321+
static void drawTitles(const Plot *plot, const ScreenTransform *transform, const Font *font, int maxYLabelWidth)
322322
{
323323
if (!plot->titles.visible)
324324
return;
@@ -333,7 +333,7 @@ static void drawTitles(const Plot *plot, const ScreenTransform *transform, int m
333333
const int titleX = clientRect.x + clientRect.width / 2 - titleWidth / 2;
334334
const int titleY = clientRect.y + clientRect.height + 2 * plot->grid.fontSize + plot->titles.fontSize;
335335

336-
DrawText(plot->titles.x, titleX, titleY, plot->titles.fontSize, *(Color *)&plot->titles.color);
336+
DrawTextEx(*font, plot->titles.x, (Vector2){titleX, titleY}, plot->titles.fontSize, 1, *(Color *)&plot->titles.color);
337337
}
338338

339339
// Vertical axis
@@ -344,7 +344,7 @@ static void drawTitles(const Plot *plot, const ScreenTransform *transform, int m
344344
const int titleX = clientRect.x - 2 * plot->grid.fontSize - plot->titles.fontSize - maxYLabelWidth;
345345
const int titleY = clientRect.y + clientRect.height / 2 + titleWidth / 2;
346346

347-
DrawTextPro(GetFontDefault(), plot->titles.y, (Vector2){titleX, titleY}, (Vector2){0, 0}, -90.0, plot->titles.fontSize, 1, *(Color *)&plot->titles.color);
347+
DrawTextPro(*font, plot->titles.y, (Vector2){titleX, titleY}, (Vector2){0, 0}, -90.0, plot->titles.fontSize, 1, *(Color *)&plot->titles.color);
348348
}
349349

350350
// Graph
@@ -355,12 +355,12 @@ static void drawTitles(const Plot *plot, const ScreenTransform *transform, int m
355355
const int titleX = clientRect.x + clientRect.width / 2 - titleWidth / 2;
356356
const int titleY = clientRect.y - 2 * plot->titles.fontSize;
357357

358-
DrawText(plot->titles.graph, titleX, titleY, plot->titles.fontSize, *(Color *)&plot->titles.color);
358+
DrawTextEx(*font, plot->titles.graph, (Vector2){titleX, titleY}, plot->titles.fontSize, 1, *(Color *)&plot->titles.color);
359359
}
360360
}
361361

362362

363-
static void drawLegend(const Plot *plot)
363+
static void drawLegend(const Plot *plot, const Font *font)
364364
{
365365
if (!plot->legend.visible)
366366
return;
@@ -401,7 +401,8 @@ static void drawLegend(const Plot *plot)
401401
const int labelX = legendRect.x + dashLength + 2 * margin;
402402
const int labelY = legendRect.y + iSeries * (plot->grid.fontSize + margin);
403403

404-
DrawText(series->name, labelX, labelY, plot->grid.fontSize, *(Color *)&plot->grid.color);
404+
DrawTextEx(*font, series->name, (Vector2){labelX, labelY}, plot->grid.fontSize, 1, *(Color *)&plot->grid.color);
405+
405406
}
406407
}
407408

@@ -429,9 +430,13 @@ void umplot_plot(UmkaStackSlot *params, UmkaStackSlot *result)
429430

430431
SetTraceLogLevel(LOG_ERROR);
431432
SetConfigFlags(FLAG_WINDOW_RESIZABLE);
432-
InitWindow(640, 480, "UmPlot");
433+
InitWindow(800, 600, "UmPlot");
433434
SetTargetFPS(30);
434435

436+
const char *fontName = "liberation.ttf";
437+
Font gridFont = LoadFontEx(fontName, plot->grid.fontSize, NULL, 256);
438+
Font titlesFont = LoadFontEx(fontName, plot->titles.fontSize, NULL, 256);
439+
435440
Rectangle clientRect = getClientRect(plot);
436441
Rectangle zoomRect = clientRect;
437442
bool showZoomRect = false;
@@ -487,16 +492,16 @@ void umplot_plot(UmkaStackSlot *params, UmkaStackSlot *result)
487492

488493
// Grid
489494
int maxYLabelWidth = 0;
490-
drawGrid(plot, &transform, &maxYLabelWidth);
495+
drawGrid(plot, &transform, &gridFont, &maxYLabelWidth);
491496

492497
// Graph
493498
drawGraph(plot, &transform);
494499

495500
// Titles
496-
drawTitles(plot, &transform, maxYLabelWidth);
501+
drawTitles(plot, &transform, &titlesFont, maxYLabelWidth);
497502

498503
// Legend
499-
drawLegend(plot);
504+
drawLegend(plot, &gridFont);
500505

501506
// Zoom rectangle
502507
if (showZoomRect)

umplot.png

2.33 KB
Loading

umplot.um

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ fn init*(numSeries: int = 1, kind: int = STYLE_LINE): Plot {
115115
plt.series[i].style = Style{kind: kind, color: defaultColors[i], width: 3.0}
116116
}
117117

118-
plt.grid = Grid{xNumLines: 5, yNumLines: 5, color: 0xFF808080, fontSize: 10, visible: true, labelled: true}
118+
plt.grid = Grid{xNumLines: 5, yNumLines: 5, color: 0xFF505050, fontSize: 12, visible: true, labelled: true}
119119
plt.titles = Titles{x: "", y: "", graph: "", color: plt.grid.color, fontSize: plt.grid.fontSize, visible: true}
120120
plt.legend = Legend{visible: true}
121121

0 commit comments

Comments
 (0)