mirror of https://github.com/buggins/dlangui.git
fonts
This commit is contained in:
parent
a78cc395bd
commit
0fa02cbcb5
|
@ -248,8 +248,12 @@
|
|||
<Folder name="core">
|
||||
<File path="src\dlangui\core\types.d" />
|
||||
</Folder>
|
||||
<Folder name="widgets">
|
||||
<File path="src\dlangui\widgets\widget.d" />
|
||||
</Folder>
|
||||
<Folder name="graphics">
|
||||
<File path="src\dlangui\graphics\drawbuf.d" />
|
||||
<File path="src\dlangui\graphics\fonts.d" />
|
||||
</Folder>
|
||||
<Folder name="platforms">
|
||||
<Folder name="common">
|
||||
|
|
|
@ -14,6 +14,8 @@ public struct Rect {
|
|||
public int top;
|
||||
public int right;
|
||||
public int bottom;
|
||||
public @property int width() { return right - left; }
|
||||
public @property int height() { return bottom - top; }
|
||||
public this(int x0, int y0, int x1, int y1) {
|
||||
left = x0;
|
||||
top = y0;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
module dlangui.graphics.drawbuf;
|
||||
|
||||
import dlangui.core.types;
|
||||
public import dlangui.core.types;
|
||||
|
||||
public uint blendARGB(uint dst, uint src, uint alpha) {
|
||||
uint srcr = (src >> 16) & 0xFF;
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
module dlangui.graphics.fonts;
|
||||
|
||||
enum FontFamily : int {
|
||||
SansSerif,
|
||||
Serif,
|
||||
MonoSpace
|
||||
}
|
||||
|
||||
class Font {
|
||||
abstract public @property int size();
|
||||
abstract public @property int height();
|
||||
abstract public @property int weight();
|
||||
abstract public @property int baseline();
|
||||
abstract public @property bool italic();
|
||||
abstract public @property string face();
|
||||
abstract public @property FontFamily family();
|
||||
abstract public @property bool isNull();
|
||||
}
|
||||
|
||||
class FontManager {
|
||||
static __gshared FontManager _instance;
|
||||
public @property void instance(FontManager manager) {
|
||||
_instance = manager;
|
||||
}
|
||||
public @property FontManager instance() {
|
||||
return _instance;
|
||||
}
|
||||
abstract public Font getFont(int size, int weight, bool italic, FontFamily family, string face);
|
||||
}
|
|
@ -1,9 +1,37 @@
|
|||
module dlangui.platforms.common.platform;
|
||||
|
||||
import dlangui.widgets.widget;
|
||||
import dlangui.graphics.drawbuf;
|
||||
|
||||
public class Window {
|
||||
int _dx;
|
||||
int _dy;
|
||||
Widget _mainWidget;
|
||||
public @property int width() { return _dx; }
|
||||
public @property int height() { return _dy; }
|
||||
public @property Widget mainWidget() { return _mainWidget; }
|
||||
public @property void mainWidget(Widget widget) {
|
||||
if (_mainWidget !is null)
|
||||
_mainWidget.window = null;
|
||||
_mainWidget = widget;
|
||||
if (_mainWidget !is null)
|
||||
_mainWidget.window = this;
|
||||
}
|
||||
abstract public void show();
|
||||
abstract public string getWindowCaption();
|
||||
abstract public void setWindowCaption(string caption);
|
||||
abstract public @property string windowCaption();
|
||||
abstract public @property void windowCaption(string caption);
|
||||
public void onResize(int width, int height) {
|
||||
_dx = width;
|
||||
_dy = height;
|
||||
if (_mainWidget !is null) {
|
||||
_mainWidget.measure(_dx, _dy);
|
||||
_mainWidget.layout(Rect(0, 0, _dx, _dy));
|
||||
}
|
||||
}
|
||||
public void onDraw(DrawBuf buf) {
|
||||
if (_mainWidget !is null)
|
||||
_mainWidget.onDraw(buf);
|
||||
}
|
||||
}
|
||||
|
||||
public class Platform {
|
||||
|
|
|
@ -10,10 +10,143 @@ import std.utf;
|
|||
import std.stdio;
|
||||
import dlangui.platforms.common.platform;
|
||||
import dlangui.graphics.drawbuf;
|
||||
import dlangui.graphics.fonts;
|
||||
|
||||
pragma(lib, "gdi32.lib");
|
||||
pragma(lib, "user32.lib");
|
||||
|
||||
class Win32Font : Font {
|
||||
HFONT _hfont;
|
||||
int _size;
|
||||
int _height;
|
||||
int _weight;
|
||||
int _baseline;
|
||||
bool _italic;
|
||||
string _face;
|
||||
FontFamily _family;
|
||||
LOGFONTA _logfont;
|
||||
Win32ColorDrawBuf _drawbuf;
|
||||
public this() {
|
||||
_drawbuf = new Win32ColorDrawBuf(1, 1);
|
||||
}
|
||||
public void clear() {
|
||||
if (_hfont !is null)
|
||||
{
|
||||
DeleteObject(_hfont);
|
||||
_hfont = NULL;
|
||||
_height = 0;
|
||||
_baseline = 0;
|
||||
_size = 0;
|
||||
}
|
||||
}
|
||||
public this(HFONT hfont, int size, int height, int weight, bool italic, string face, FontFamily family, int baseline) {
|
||||
_hfont = hfont;
|
||||
_size = size;
|
||||
_height = height;
|
||||
_weight = weight;
|
||||
_baseline = baseline;
|
||||
_italic = italic;
|
||||
_face = face;
|
||||
_family = family;
|
||||
}
|
||||
public @property override int size() { return _size; }
|
||||
public @property override int height() { return _height; }
|
||||
public @property override int weight() { return _weight; }
|
||||
public @property override int baseline() { return _baseline; }
|
||||
public @property override bool italic() { return _italic; }
|
||||
public @property override string face() { return _face; }
|
||||
public @property override FontFamily family() { return _family; }
|
||||
public @property override bool isNull() { return _hfont !is null; }
|
||||
public bool create(const LOGFONTA * logfont) {
|
||||
if (!isNull())
|
||||
clear();
|
||||
_logfont = *logfont;
|
||||
_hfont = CreateFontIndirectA(logfont);
|
||||
if (!_hfont)
|
||||
return false;
|
||||
//memcpy( &_logfont, &lf, sizeof(LOGFONT) );
|
||||
// get text metrics
|
||||
SelectObject(_drawbuf.dc, _hfont);
|
||||
TEXTMETRICW tm;
|
||||
GetTextMetricsW(_drawbuf.dc, &tm);
|
||||
_logfont.lfHeight = tm.tmHeight;
|
||||
_logfont.lfWeight = tm.tmWeight;
|
||||
_logfont.lfItalic = tm.tmItalic;
|
||||
_logfont.lfCharSet = tm.tmCharSet;
|
||||
GetTextFaceA(_drawbuf.dc, _logfont.lfFaceName.length - 1, _logfont.lfFaceName.ptr);
|
||||
_height = tm.tmHeight;
|
||||
_baseline = _height - tm.tmDescent;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
class Win32FontManager : FontManager {
|
||||
public this() {
|
||||
instance = this;
|
||||
init();
|
||||
}
|
||||
public bool init() {
|
||||
Win32ColorDrawBuf drawbuf = new Win32ColorDrawBuf(1,1);
|
||||
LOGFONTA lf;
|
||||
lf.lfCharSet = ANSI_CHARSET;
|
||||
int res =
|
||||
EnumFontFamiliesExA(
|
||||
drawbuf.dc, // handle to DC
|
||||
&lf, // font information
|
||||
&LVWin32FontEnumFontFamExProc, // callback function (FONTENUMPROC)
|
||||
cast(LPARAM)(cast(void*)this), // additional data
|
||||
0U // not used; must be 0
|
||||
);
|
||||
|
||||
return res!=0;
|
||||
}
|
||||
public override Font getFont(int size, int weight, bool italic, FontFamily family, string face) {
|
||||
// TODO:
|
||||
return null;
|
||||
}
|
||||
public bool registerFont(const LOGFONTA * logfont) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// definition
|
||||
//extern(Windows) {
|
||||
int LVWin32FontEnumFontFamExProc(
|
||||
const (LOGFONTA) *lf, // logical-font data
|
||||
const (TEXTMETRICA) *lpntme, // physical-font data
|
||||
//ENUMLOGFONTEX *lpelfe, // logical-font data
|
||||
//NEWTEXTMETRICEX *lpntme, // physical-font data
|
||||
DWORD FontType, // type of font
|
||||
LPARAM lParam // application-defined data
|
||||
)
|
||||
{
|
||||
//
|
||||
if (FontType == TRUETYPE_FONTTYPE)
|
||||
{
|
||||
void * p = cast(void*)lParam;
|
||||
Win32FontManager fontman = cast(Win32FontManager)p;
|
||||
Win32Font fnt;
|
||||
//if (strcmp(lf->lfFaceName, "Courier New"))
|
||||
// return 1;
|
||||
if (fnt.create(lf))
|
||||
{
|
||||
//
|
||||
wchar chars[] = [0, 0xBF, 0xE9, 0x106, 0x410, 0x44F, 0];
|
||||
for (int i=0; chars[i]; i++)
|
||||
{
|
||||
//LVFont::glyph_info_t glyph;
|
||||
//if (!fnt.getGlyphInfo( chars[i], &glyph, L' ' )) //def_char
|
||||
// return 1;
|
||||
}
|
||||
fontman.registerFont(lf); //&lpelfe->elfLogFont
|
||||
}
|
||||
fnt.clear();
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
//}
|
||||
|
||||
extern (C) int UIAppMain();
|
||||
|
||||
immutable WIN_CLASS_NAME = "DLANGUI_APP";
|
||||
|
@ -54,16 +187,19 @@ class Win32Window : Window {
|
|||
ShowWindow(_hwnd, _cmdShow);
|
||||
UpdateWindow(_hwnd);
|
||||
}
|
||||
public override string getWindowCaption() {
|
||||
public override @property string windowCaption() {
|
||||
return _caption;
|
||||
}
|
||||
public override void setWindowCaption(string caption) {
|
||||
public override @property void windowCaption(string caption) {
|
||||
_caption = caption;
|
||||
SetWindowTextW(_hwnd, toUTF16z(_caption));
|
||||
}
|
||||
public void onCreate() {
|
||||
writeln("Window onCreate");
|
||||
}
|
||||
public void onDestroy() {
|
||||
writeln("Window onDestroy");
|
||||
}
|
||||
}
|
||||
|
||||
class Win32Platform : Platform {
|
||||
|
@ -265,14 +401,19 @@ LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
|
|||
}
|
||||
//PlaySoundA("hellowin.wav", NULL, SND_FILENAME | SND_ASYNC);
|
||||
return 0;
|
||||
case WM_WINDOWPOSCHANGED:
|
||||
{
|
||||
WINDOWPOS * pos = cast(WINDOWPOS*)lParam;
|
||||
window.onResize(pos.cx, pos.cy);
|
||||
}
|
||||
return 0;
|
||||
|
||||
case WM_PAINT:
|
||||
{
|
||||
hdc = BeginPaint(hwnd, &ps);
|
||||
Win32ColorDrawBuf buf = window.getDrawBuf();
|
||||
buf.clear(0x808080);
|
||||
buf.fillRect(40, 40, 200, 200, 0xFF8000);
|
||||
buf.fillRect(150, 120, 500, 400, 0xFF80FF);
|
||||
window.onDraw(buf);
|
||||
buf.drawTo(hdc, 0, 0);
|
||||
//drawBuf2DC(hdc, 0, 0, buf);
|
||||
scope(exit) EndPaint(hwnd, &ps);
|
||||
|
@ -281,6 +422,7 @@ LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
|
|||
return 0;
|
||||
|
||||
case WM_DESTROY:
|
||||
window.onDestroy();
|
||||
PostQuitMessage(0);
|
||||
return 0;
|
||||
|
||||
|
|
|
@ -0,0 +1,78 @@
|
|||
module dlangui.widgets.widget;
|
||||
|
||||
import dlangui.core.types;
|
||||
import dlangui.graphics.drawbuf;
|
||||
import dlangui.platforms.common.platform;
|
||||
|
||||
public class Widget {
|
||||
Rect _pos;
|
||||
Rect _margins;
|
||||
Rect _padding;
|
||||
int _measuredWidth;
|
||||
int _measuredHeight;
|
||||
bool _needLayout;
|
||||
bool _needDraw;
|
||||
Widget _parent;
|
||||
Window _window;
|
||||
|
||||
uint _backgroundColor = 0xC0C0C0;
|
||||
|
||||
public this() {
|
||||
_needLayout = true;
|
||||
_needDraw = true;
|
||||
}
|
||||
public @property Rect margins() { return _margins; }
|
||||
public @property void margins(Rect rc) { _margins = rc; }
|
||||
public @property Rect padding() { return _padding; }
|
||||
public @property void padding(Rect rc) { _padding = rc; }
|
||||
public @property uint backgroundColor() { return _backgroundColor; }
|
||||
public @property void backgroundColor(uint color) { _backgroundColor = color; }
|
||||
public @property bool needLayout() { return _needLayout; }
|
||||
public @property bool needDraw() { return _needDraw; }
|
||||
public @property int childCount() { return 0; }
|
||||
public Widget child(int index) { return null; }
|
||||
public @property Widget parent() { return _parent; }
|
||||
public @property void parent(Widget parent) { _parent = parent; }
|
||||
public @property Window window() {
|
||||
Widget p = this;
|
||||
while (p !is null) {
|
||||
if (p._window !is null)
|
||||
return p._window;
|
||||
p = p.parent;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public @property void window(Window window) { _window = window; }
|
||||
public @property measuredWidth() { return _measuredWidth; }
|
||||
public @property measuredHeight() { return _measuredHeight; }
|
||||
public void measure(int width, int height) {
|
||||
_measuredWidth = _measuredHeight = 0;
|
||||
}
|
||||
public void layout(Rect rc) {
|
||||
_pos = rc;
|
||||
}
|
||||
public @property int width() { return _pos.width; }
|
||||
public @property int height() { return _pos.height; }
|
||||
public void applyMargins(ref Rect rc) {
|
||||
Rect m = margins;
|
||||
rc.left += m.left;
|
||||
rc.top += m.top;
|
||||
rc.bottom -= m.bottom;
|
||||
rc.right -= m.right;
|
||||
}
|
||||
public void applyPadding(ref Rect rc) {
|
||||
Rect m = padding;
|
||||
rc.left += m.left;
|
||||
rc.top += m.top;
|
||||
rc.bottom -= m.bottom;
|
||||
rc.right -= m.right;
|
||||
}
|
||||
public void onDraw(DrawBuf buf) {
|
||||
Rect rc = _pos;
|
||||
applyMargins(rc);
|
||||
buf.fillRect(_pos, _backgroundColor);
|
||||
applyPadding(rc);
|
||||
buf.fillRect(rc.left + rc.width / 2, rc.top, rc.left + rc.width / 2 + 2, rc.bottom, 0xFF8000);
|
||||
buf.fillRect(rc.left, rc.top + rc.height / 2, rc.right, rc.top + rc.height / 2 + 2, 0xFF80FF);
|
||||
}
|
||||
}
|
|
@ -1,11 +1,14 @@
|
|||
module winmain;
|
||||
|
||||
import dlangui.platforms.common.platform;
|
||||
import dlangui.widgets.widget;
|
||||
|
||||
|
||||
extern (C) int UIAppMain() {
|
||||
Window window = Platform.instance().createWindow("My Window", null);
|
||||
Widget myWidget = new Widget();
|
||||
window.mainWidget = myWidget;
|
||||
window.show();
|
||||
window.setWindowCaption("New Window Caption");
|
||||
window.windowCaption = "New Window Caption";
|
||||
return Platform.instance().enterMessageLoop();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue