Skip to content

Commit

Permalink
windows: implement gral_draw_context_add_text
Browse files Browse the repository at this point in the history
  • Loading branch information
eyelash committed Nov 9, 2024
1 parent e4492c1 commit 3189b00
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ libgral is a low-level abstraction layer exposing a unified C API to open window
![Linux screenshot](https://github.com/user-attachments/assets/5854a18c-8a36-41e5-bb98-bf025266fe46)

![macOS screenshot](https://github.com/user-attachments/assets/6a34fc32-52c2-4c3f-96ee-5f1bf3e2fc95)

![Windows screenshot](https://github.com/user-attachments/assets/c55333e1-5f19-4647-80d6-b5cad8ee2c5c)
44 changes: 34 additions & 10 deletions gral_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -602,20 +602,36 @@ class ColorDrawingEffect: public IUnknown {

class GralTextRenderer: public IDWriteTextRenderer {
ULONG reference_count;
D2D1_COLOR_F color;
ComPointer<ID2D1SolidColorBrush> brush;
public:
GralTextRenderer(D2D1_COLOR_F const &color): reference_count(1), color(color) {}
GralTextRenderer(ComPointer<ID2D1SolidColorBrush> const &brush): reference_count(1), brush(brush) {}
IFACEMETHOD(DrawGlyphRun)(void *clientDrawingContext, FLOAT baselineOriginX, FLOAT baselineOriginY, DWRITE_MEASURING_MODE measuringMode, DWRITE_GLYPH_RUN const *glyphRun, DWRITE_GLYPH_RUN_DESCRIPTION const *glyphRunDescription, IUnknown *clientDrawingEffect) {
gral_draw_context *draw_context = (gral_draw_context *)clientDrawingContext;
ComPointer<ID2D1SolidColorBrush> brush;
if (clientDrawingEffect) {
ColorDrawingEffect *color_drawing_effect = (ColorDrawingEffect *)clientDrawingEffect;
draw_context->target->CreateSolidColorBrush(color_drawing_effect->get_color(), &brush);
if (brush) {
if (clientDrawingEffect) {
ColorDrawingEffect *color_drawing_effect = (ColorDrawingEffect *)clientDrawingEffect;
ComPointer<ID2D1SolidColorBrush> effect_brush;
draw_context->target->CreateSolidColorBrush(color_drawing_effect->get_color(), &effect_brush);
draw_context->target->DrawGlyphRun(D2D1::Point2F(baselineOriginX, baselineOriginY), glyphRun, effect_brush, measuringMode);
}
else {
draw_context->target->DrawGlyphRun(D2D1::Point2F(baselineOriginX, baselineOriginY), glyphRun, brush, measuringMode);
}
}
else {
draw_context->target->CreateSolidColorBrush(color, &brush);
ComPointer<ID2D1PathGeometry> path;
factory->CreatePathGeometry(&path);
ComPointer<ID2D1GeometrySink> sink;
path->Open(&sink);
glyphRun->fontFace->GetGlyphRunOutline(glyphRun->fontEmSize, glyphRun->glyphIndices, glyphRun->glyphAdvances, glyphRun->glyphOffsets, glyphRun->glyphCount, glyphRun->isSideways, glyphRun->bidiLevel % 2, sink);
sink->Close();
if (draw_context->open) {
draw_context->sink->EndFigure(D2D1_FIGURE_END_OPEN);
draw_context->open = false;
}
path->Outline(D2D1::Matrix3x2F(1.0f, 0.0f, 0.0f, 1.0f, baselineOriginX, baselineOriginY), draw_context->sink);
draw_context->sink->SetFillMode(D2D1_FILL_MODE_WINDING);
}
draw_context->target->DrawGlyphRun(D2D1::Point2F(baselineOriginX, baselineOriginY), glyphRun, brush, measuringMode);
return S_OK;
}
IFACEMETHOD(DrawInlineObject)(void *clientDrawingContext, FLOAT originX, FLOAT originY, IDWriteInlineObject *inlineObject, BOOL isSideways, BOOL isRightToLeft, IUnknown *clientDrawingEffect) {
Expand Down Expand Up @@ -792,13 +808,21 @@ void gral_draw_context_draw_text(gral_draw_context *draw_context, gral_text *tex
DWRITE_LINE_METRICS line_metrics;
UINT32 count = 1;
text->layout->GetLineMetrics(&line_metrics, count, &count);
ComPointer<ID2D1SolidColorBrush> brush;
draw_context->target->CreateSolidColorBrush(D2D1::ColorF(red, green, blue, alpha), &brush);
ComPointer<GralTextRenderer> renderer;
*&renderer = new GralTextRenderer(D2D1::ColorF(red, green, blue, alpha));
*&renderer = new GralTextRenderer(brush);
text->layout->Draw(draw_context, renderer, x, y-line_metrics.baseline);
}

void gral_draw_context_add_text(gral_draw_context *draw_context, gral_text *text, float x, float y) {
// TODO: implement
DWRITE_LINE_METRICS line_metrics;
UINT32 count = 1;
text->layout->GetLineMetrics(&line_metrics, count, &count);
ComPointer<ID2D1SolidColorBrush> brush;
ComPointer<GralTextRenderer> renderer;
*&renderer = new GralTextRenderer(brush);
text->layout->Draw(draw_context, renderer, x, y-line_metrics.baseline);
}

void gral_draw_context_close_path(gral_draw_context *draw_context) {
Expand Down

0 comments on commit 3189b00

Please sign in to comment.