Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
RFont.cxx
Go to the documentation of this file.
1/*************************************************************************
2 * Copyright (C) 1995-2021, Rene Brun and Fons Rademakers. *
3 * All rights reserved. *
4 * *
5 * For the licensing terms see $ROOTSYS/LICENSE. *
6 * For the list of contributors see $ROOTSYS/README/CREDITS. *
7 *************************************************************************/
8
9#include "ROOT/RFont.hxx"
10
11#include "ROOT/RDisplayItem.hxx"
12
13#include "TSystem.h"
14#include "TString.h"
15#include "TBase64.h"
16
17#include <fstream>
18
19
20using namespace ROOT::Experimental;
21
22////////////////////////////////////////////////////////////////////////
23/// Set font source as URL.
24/// Can be external url or path in the ROOT THttpServer
25/// By default woff2 format is supposed
26
27void RFont::SetUrl(const std::string &url, const std::string &fmt)
28{
29 if (url.empty())
30 SetSrc("");
31 else
32 SetSrc(TString::Format("url('%s') format('%s')", url.c_str(), fmt.c_str()).Data());
33}
34
35////////////////////////////////////////////////////////////////////////
36/// Set font source as file content.
37/// Font file will be immediately read and converted in base64 string
38/// By default woff2 format is supposed
39
40void RFont::SetFile(const std::string &fname, const std::string &fmt)
41{
42 SetSrc("");
43
44 if (fname.empty())
45 return;
46
47 TString fullname = fname;
48 gSystem->ExpandPathName(fullname);
49
50 if (!gSystem->AccessPathName(fullname.Data(), kReadPermission)) {
51 std::ifstream is(fullname.Data(), std::ios::in | std::ios::binary);
52 std::string res;
53 if (is) {
54 is.seekg(0, std::ios::end);
55 res.resize(is.tellg());
56 is.seekg(0, std::ios::beg);
57 is.read((char *)res.data(), res.length());
58 if (!is)
59 res.clear();
60 }
61
62 if (!res.empty()) {
63 TString base64 = TBase64::Encode(res.c_str(), res.length());
64 SetSrc(TString::Format("url('data:application/font-%s;charset=utf-8;base64,%s') format('%s')", fmt.c_str(), base64.Data(), fmt.c_str()).Data());
65 }
66 }
67}
68
69////////////////////////////////////////////////////////////////////////
70/// Set src attribute of font-face directly
71/// Only for expert use
72
73void RFont::SetSrc(const std::string &src)
74{
75 fSrc = src;
76}
@ kReadPermission
Definition TSystem.h:55
externTSystem * gSystem
Definition TSystem.h:582
void SetSrc(const std::string &src)
Set src attribute of font-face directly Only for expert use.
Definition RFont.cxx:73
void SetFile(const std::string &fname, const std::string &fmt="woff2")
Set font source as file content.
Definition RFont.cxx:40
void SetUrl(const std::string &url, const std::string &fmt="woff2")
Set font source as URL.
Definition RFont.cxx:27
std::string fSrc
font source, assigned as "src" attribute
Definition RFont.hxx:33
static TString Encode(const char *data)
Transform data into a null terminated base64 string.
Definition TBase64.cxx:106
Basic string class.
Definition TString.h:138
const char * Data() const
Definition TString.h:384
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition TString.cxx:2385
TString base64
Namespace for ROOT features in testing.
Definition TROOT.h:100