Skip to content

Instantly share code, notes, and snippets.

@speedie1337
Created March 9, 2024 01:08
Show Gist options
  • Save speedie1337/504e6d6b74651ce444ceca00d6173f50 to your computer and use it in GitHub Desktop.
Save speedie1337/504e6d6b74651ce444ceca00d6173f50 to your computer and use it in GitHub Desktop.
Drawing text using skia after the removal of RefDefault()
#include "include/gpu/GrBackendSurface.h"
#include "include/gpu/GrDirectContext.h"
#include "include/gpu/gl/GrGLInterface.h"
#include "include/gpu/gl/GrGLAssembleInterface.h"
#include "include/gpu/ganesh/SkSurfaceGanesh.h"
#include "include/gpu/ganesh/gl/GrGLBackendSurface.h"
#include "include/gpu/ganesh/gl/GrGLDirectContext.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkColor.h"
#include "include/core/SkColorSpace.h"
#include "include/core/SkSurface.h"
#include "include/core/SkTextBlob.h"
#include "include/core/SkFont.h"
#include "include/core/SkFontStyle.h"
#include "include/core/SkTypeface.h"
#include "include/core/SkFontMgr.h"
#include "include/ports/SkFontConfigInterface.h"
#include "include/ports/SkFontMgr_FontConfigInterface.h"
class Font {
private:
SkFont fnt;
SkFontStyle fs;
sk_sp<SkFontMgr> mgr;
sk_sp<SkFontConfigInterface> fci;
sk_sp<SkTypeface> tf;
int fontStyle{-1};
int antiAlias{-1};
std::string fontName{"Default"};
protected:
public:
enum {
FONT_ALIAS,
FONT_ANTIALIAS,
FONT_ANTIALIAS_SUBPIXEL,
FONT_STYLE_ANY,
FONT_STYLE_NORMAL,
FONT_STYLE_BOLD,
FONT_STYLE_ITALIC,
FONT_STYLE_BOLDITALIC,
};
void setSize(int s){fnt.setSize(s);}
void setTypeface(const std::string& typeface) {
if (fontStyle == FONT_STYLE_NORMAL) {
fs = SkFontStyle::Normal();
} else if (fontStyle == FONT_STYLE_BOLD) {
fs = SkFontStyle::Bold();
} else if (fontStyle == FONT_STYLE_ITALIC) {
fs = SkFontStyle::Italic();
} else if (fontStyle == FONT_STYLE_BOLDITALIC) {
fs = SkFontStyle::BoldItalic();
}
fci = SkFontConfigInterface::RefGlobal();
mgr = fci ? SkFontMgr_New_FCI(std::move(fci)) : nullptr;
tf = mgr->legacyMakeTypeface(typeface.c_str(), fs);
fnt.setTypeface(tf);
fontName = typeface;
}
void setAntiAlias(int antialias){fnt.setEdging(antialias == FONT_ANTIALIAS ? SkFont::Edging::kAntiAlias : antialias == FONT_ALIAS ? SkFont::Edging::kAlias : SkFont::Edging::kSubpixelAntiAlias); antiAlias = antialias;}
void setFontStyle(int fontstyle){fontStyle = fontstyle;}
SkFont get(){return fnt;}
std::string getFontName(){return fontName;}
int getFontStyle(){return fontStyle;}
int getAntiAlias(){return antiAlias;}
Font(){}
Font(const std::string& typeface, int size, int style, int antialias){setSize(size); setAntiAlias(antialias); setFontStyle(style); setTypeface(typeface);}
~Font(){ if (tf != nullptr) tf->unref(); }
};
class TextProperties {
private:
int x{};
int y{};
int w{};
int h{};
int s{};
int style{};
int alias{};
std::string fontName{};
protected:
public:
int getTextX(){return x;}
int getTextY(){return y;}
int getTextWidth(){return w;}
int getTextHeight(){return w;}
int getFontSize(){return s;}
int getFontStyle(){return style;}
int getAntiAlias(){return alias;}
std::string getFontName(){return fontName;}
void setTextX(int _x){x = _x;}
void setTextY(int _y){y = _y;}
void setTextWidth(int _w){w = _w;}
void setTextHeight(int _h){h = _h;}
void setFontSize(int _s){s = _s;}
void setFontName(const std::string& _fontName){fontName = _fontName;}
void setFontStyle(int _style){style = _style;}
void setAntiAlias(int _alias){alias = _alias;}
};
SkCanvas* returnCanvas(){return canvas;}
TextProperties drawText(const std::string& t, Paint p, Font f, int x, int y){
returnCanvas()->drawSimpleText(t.c_str(), t.length(), SkTextEncoding::kUTF8, x, y, f.get(), p);
SkRect ms{};
f.get().measureText(t.c_str(), t.length(), SkTextEncoding::kUTF8, &ms);
TextProperties prop;
prop.setTextX(ms.x());
prop.setTextY(ms.y());
prop.setTextWidth(ms.width());
prop.setTextHeight(ms.height());
prop.setFontSize(f.get().getSize());
prop.setFontName(f.getFontName());
prop.setFontStyle(f.getFontStyle());
prop.setAntiAlias(f.getAntiAlias());
return prop;
};
This took an extremely long time to figure out. Recently Skia removed many deprecated features, but also one noteworthy function, that one being RefDefault().
Because of this, text will draw using empty glyphs, which isn't nice. The solution isn't documented anywhere, but I looked at Chromium source code and pieced together
something that works. I am not that experienced with Skia, so I don't know if this is ideal, but it does the trick.
Note that there's a lot of unnecessary code here, I just copy pasted some wrapper classes from my current project. The noteworthy code is in setTypeface() and drawText().
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment