summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorAlexander Neumann <an2048@gmail.com>2019-05-06 10:38:33 +0200
committerGitHub <noreply@github.com>2019-05-06 10:38:33 +0200
commitec888cca4a2f8b084a5575449ccb806a50e73ece (patch)
tree2e2e3ecf26120cb0f9c74ef57dcbc1230f4b6124
parentMerge branch 'master' of github.com:lxn/walk (diff)
parentReplace call to deprecated StringToUTF16Ptr() with UTF16PtrFromString() (diff)
downloadwireguard-windows-ec888cca4a2f8b084a5575449ccb806a50e73ece.tar.xz
wireguard-windows-ec888cca4a2f8b084a5575449ccb806a50e73ece.zip
Merge pull request #500 from dfbag7/add-font-mem-resource
Add FontMemResource type
-rw-r--r--AUTHORS1
-rw-r--r--fontresource.go83
2 files changed, 84 insertions, 0 deletions
diff --git a/AUTHORS b/AUTHORS
index ce5e8663..b44a7a88 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -29,3 +29,4 @@ Semyon Tokarev <zlobzn@gmail.com>
Shawn Sun <datago@yeah.net>
Tim Dufrane <tim.dufrane@gmail.com>
Vincent Vanackere <vincent.vanackere@gmail.com>
+Dmitry Bagdanov <dimbojob@gmail.com>
diff --git a/fontresource.go b/fontresource.go
new file mode 100644
index 00000000..1496232a
--- /dev/null
+++ b/fontresource.go
@@ -0,0 +1,83 @@
+// Copyright 2010 The Walk Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// +build windows
+
+package walk
+
+import (
+ "github.com/lxn/win"
+ "syscall"
+)
+
+// FontMemResource represents a font resource loaded into memory from
+// the application's resources.
+type FontMemResource struct {
+ hFontResource win.HANDLE
+}
+
+func newFontMemResource(resourceName *uint16) (*FontMemResource, error) {
+ hModule := win.HMODULE(win.GetModuleHandle(nil))
+ if hModule == win.HMODULE(0) {
+ return nil, lastError("GetModuleHandle")
+ }
+
+ hres := win.FindResource(hModule, resourceName, win.MAKEINTRESOURCE(8) /*RT_FONT*/)
+ if hres == win.HRSRC(0) {
+ return nil, lastError("FindResource")
+ }
+
+ size := win.SizeofResource(hModule, hres)
+ if size == 0 {
+ return nil, lastError("SizeofResource")
+ }
+
+ hResLoad := win.LoadResource(hModule, hres)
+ if hResLoad == win.HGLOBAL(0) {
+ return nil, lastError("LoadResource")
+ }
+
+ ptr := win.LockResource(hResLoad)
+ if ptr == 0 {
+ return nil, lastError("LockResource")
+ }
+
+ numFonts := uint32(0)
+ hFontResource := win.AddFontMemResourceEx(ptr, size, nil, &numFonts)
+
+ if hFontResource == win.HANDLE(0) || numFonts == 0 {
+ return nil, lastError("AddFontMemResource")
+ }
+
+ return &FontMemResource { hFontResource: hFontResource }, nil
+}
+
+// NewFontMemResourceByName function loads a font resource from the executable's resources
+// using the resource name.
+// The font must be embedded into resources using corresponding operator in the
+// application's RC script.
+func NewFontMemResourceByName(name string) (*FontMemResource, error) {
+ lpstr, err := syscall.UTF16PtrFromString(name)
+ if err != nil {
+ return nil, err
+ }
+
+ return newFontMemResource(lpstr)
+}
+
+// NewFontMemResourceById function loads a font resource from the executable's resources
+// using the resource ID.
+// The font must be embedded into resources using corresponding operator in the
+// application's RC script.
+func NewFontMemResourceById(id int) (*FontMemResource, error) {
+ return newFontMemResource(win.MAKEINTRESOURCE(uintptr(id)))
+}
+
+// Dispose removes the font resource from memory
+func (fmr *FontMemResource) Dispose() {
+ if fmr.hFontResource != 0 {
+ win.RemoveFontMemResourceEx(fmr.hFontResource)
+ fmr.hFontResource = 0
+ }
+}