Skip to content

Commit

Permalink
WIP font
Browse files Browse the repository at this point in the history
  • Loading branch information
XITRIX committed Dec 7, 2024
1 parent 6b4fb0e commit 6b2d6d0
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 20 deletions.
23 changes: 11 additions & 12 deletions Submodules/UIKit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ add_definitions(
-DSK_TRIVIAL_ABI=[[clang::trivial_abi]]
)

add_library(UIKit
lib/platforms/SkiaCtx.cpp
lib/Application.cpp
lib/NXSize.cpp
)

# APPLE
if (APPLE)
list(APPEND platform_sources
target_sources(UIKit PRIVATE
lib/platforms/SkiaCtx_sdlBase.cpp
)

Expand All @@ -20,7 +26,7 @@ if (APPLE)
${EXTERN_PATH}/skia/out/mac-arm64-angle/libwindow.a
)

list(APPEND platform_sources
target_sources(UIKit PRIVATE
lib/platforms/apple/macos/SkiaCtx_macos.mm
)
# IOS
Expand All @@ -37,19 +43,12 @@ if (APPLE)
${EXTERN_PATH}/skia/out/ios-arm64-angle/libwindow.a
)

list(APPEND platform_sources
lib/platforms/apple/ios/SkiaCtx_ios.mm
target_sources(UIKit PRIVATE
lib/platforms/apple/ios/SkiaCtx_ios.mm
)
endif ()
endif ()

add_library(UIKit
lib/platforms/SkiaCtx.cpp
lib/Application.cpp
lib/NXSize.cpp
${platform_sources}
)

target_include_directories(UIKit PUBLIC
${EXTERN_PATH}/SDL/include
${EXTERN_PATH}/skia
Expand All @@ -64,7 +63,7 @@ elseif (PLATFORM_IOS)
endif ()

if (APPLE)
list(APPEND platform_libs "-framework Foundation" "-framework VideoToolbox" "-framework AVKit" "-framework MetalKit")
list(APPEND platform_libs "-framework Foundation" "-framework VideoToolbox" "-framework AVKit" "-framework MetalKit" "-framework CoreText")
endif ()

target_link_libraries(UIKit PRIVATE ${platform_libs})
5 changes: 3 additions & 2 deletions Submodules/UIKit/include/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ class Application {

private:
SDL_Window* window;
SDL_Renderer* renderer;

// std::unique_ptr<skwindow::WindowContext> skiaWindow;
sk_sp<SkTypeface> typeface;
float fRotationAngle = 0;

std::unique_ptr<SkiaCtx> skiaCtx;

static Application* shared;
Expand Down
9 changes: 7 additions & 2 deletions Submodules/UIKit/include/SkiaCtx.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,29 @@
#include <memory>
#include "include/core/SkSurface.h"
#include <functional>
#include "include/core/SkFontMgr.h"

namespace NXKit {

bool platformRunLoop(std::function<bool()> loop);

class SkiaCtx {
public:
virtual ~SkiaCtx() {}
virtual sk_sp<SkSurface> getBackbufferSurface() = 0;
virtual sk_sp<GrDirectContext> directContext() = 0;
virtual void flushAndSubmit(sk_sp<SkSurface> surface);
virtual NXSize getSize() = 0;
virtual float getScaleFactor() { return 1; }
virtual void swapBuffers() = 0;

virtual float getScaleFactor() { return 1; }
virtual sk_sp<SkFontMgr> getFontMgr() { return fontMgr; }

protected:
NXSize _size;
sk_sp<SkFontMgr> fontMgr;
};

std::unique_ptr<SkiaCtx> MakeSkiaCtx(SDL_Window* window);

}
}
4 changes: 3 additions & 1 deletion Submodules/UIKit/include/platforms/ios/SkiaCtx_ios.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <platforms/SkiaCtx_sdlBase.h>
#include <include/gpu/ganesh/GrDirectContext.h>

#include "include/core/SkFont.h"

namespace NXKit {

class SkiaCtx_ios : public SkiaCtx_sdlBase {
Expand All @@ -22,4 +24,4 @@ class SkiaCtx_ios : public SkiaCtx_sdlBase {
void initContext();
};

}
}
32 changes: 29 additions & 3 deletions Submodules/UIKit/lib/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
#include <include/core/SkSurface.h>
#include <include/core/SkRRect.h>
#include <include/core/SkCanvas.h>
#include <include/core/SkTypeface.h>

// SKIA METAL GPU
#include "include/gpu/ganesh/GrBackendSurface.h"

#include "include/effects/SkGradientShader.h"
#include "include/effects/SkImageFilters.h"
#include "include/core/SkFont.h"

using namespace NXKit;

Expand Down Expand Up @@ -57,12 +59,14 @@ Application::Application() {

SDL_AddEventWatch(resizingEventWatcher, window);

SkFontStyle style;
typeface = skiaCtx->getFontMgr()->matchFamilyStyle(nullptr, style);
// SkTypeface::Make

while(platformRunLoop([this]() { return runLoop(); }));
}

Application::~Application() {
skiaCtx = nullptr;
}
Application::~Application() {}

bool Application::runLoop() {
SDL_Event event;
Expand Down Expand Up @@ -101,6 +105,28 @@ void Application::render() {
rect = SkRect::MakeXYWH(410, 10, 212, 4000);
canvas->drawRect(rect, paint);

SkFont font(typeface);
font.setSubpixel(true);
font.setSize(49);
paint.setColor(SK_ColorBLACK);
//
canvas->save();
static const char message[] = "Hello World ";

// Translate and rotate
canvas->translate(300, 300);
fRotationAngle += 0.2f;
if (fRotationAngle > 360) {
fRotationAngle -= 360;
}
canvas->rotate(fRotationAngle);

// Draw the text
canvas->drawString(message, 200, 20, font, paint);
// canvas->drawSimpleText(message, strlen(message), SkTextEncoding::kUTF8, 0, 0, font, paint);

canvas->restore();

// Set up a linear gradient and draw a circle
{
SkPoint linearPoints[] = { { 0, 0 }, { 300, 300 } };
Expand Down
4 changes: 4 additions & 0 deletions Submodules/UIKit/lib/platforms/apple/ios/SkiaCtx_ios.mm
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include "include/gpu/ganesh/gl/GrGLInterface.h"
#include "include/private/base/SkTemplates.h"

#include "include/ports/SkFontMgr_mac_ct.h"

#include <GLES3/gl3.h>
#include <UIKit/UIKit.h>
#include <dlfcn.h>
Expand All @@ -22,6 +24,8 @@
SkiaCtx_ios::SkiaCtx_ios(SDL_Window* window): SkiaCtx_sdlBase(window) {
SkGraphics::Init();
initContext();

fontMgr = SkFontMgr_New_CoreText(nullptr);
}

void SkiaCtx_ios::initContext() {
Expand Down
4 changes: 4 additions & 0 deletions Submodules/UIKit/lib/platforms/apple/macos/SkiaCtx_macos.mm
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include "include/gpu/ganesh/gl/GrGLInterface.h"
#include "include/private/base/SkTemplates.h"

#include "include/ports/SkFontMgr_mac_ct.h"

#include <GLES3/gl3.h>
#include <Cocoa/Cocoa.h>
#include <dlfcn.h>
Expand All @@ -22,6 +24,8 @@
SkiaCtx_macos::SkiaCtx_macos(SDL_Window* window): SkiaCtx_sdlBase(window) {
SkGraphics::Init();
initContext();

fontMgr = SkFontMgr_New_CoreText(nullptr);
}

void SkiaCtx_macos::initContext() {
Expand Down

0 comments on commit 6b2d6d0

Please sign in to comment.