resources

package
Version: v0.0.0-...-770a60e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 10, 2021 License: BSD-3-Clause Imports: 26 Imported by: 0

Documentation

Overview

Package resources resolves all kinds of resources for an application.

As resource loading may be a time-consuming task, some functions in this package will work in an async/await fashion by returning a promise. Functions named

Resolve…(…)

will return a resource-specific promise type, which the client will call later to receive the loaded resource. The call to the promise-function will then block until loading has completed.

BSD 3-Clause License

Copyright (c) 2020–21, Norbert Pillmayer

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CacheDirPath

func CacheDirPath(subfolders ...string) (string, error)

CacheDirPath checks and possibly creates a folder in the user's cache directory. The base cache directory is taken from `os.UserCacheDir()`, plus an application specific key, taken as `app-key` from the global configuration. Clients may specify a sequence of folder names, which will be appended to the base cache path. Non-existing sub-folders will be created as necessary (with permissions 755).

Returns the path to the cache-(sub-)folder or an error.

func CacheGoogleFont

func CacheGoogleFont(fi GoogleFontInfo, variant string) (filepath string, err error)

CacheGoogleFont loads a font described by fi with a given variant. The loaded font is cached in the user's cache directory.

func DownloadCachedFile

func DownloadCachedFile(filepath string, url string) error

DownloadFile will download a url to a local file (usually located in the user's cache directory).

func FindLocalFont

func FindLocalFont(pattern string, style xfont.Style, weight xfont.Weight) (
	desc font.Descriptor, variant string)

FindLocalFont searches for a locally installed font variant.

If present and configured, FindLocalFont will be using the fontconfig system (https://www.freedesktop.org/wiki/Software/fontconfig/). fontconfig has to be configured in the global application setup by pointing to the absolute path of the 'fc-list' binary.

We will copy the output of fc-list to the user's config directory once. Subsequent calls will use the cached entries to search for a font, given a name pattern, a style and a weight. We call the binary instead of using the C library because of possible version issues and to reduce compile-time dependencies.

If fontconfig is not configured, FindLocalFont will fall back to scanning the system's fonts-folders (OS dependent).

(Please refer to function `ResolveTypeCase`, too)

func ListGoogleFonts

func ListGoogleFonts(pattern string)

ListGoogleFonts produces a listing of available fonts from the Google webfont service, with font-family names matching a given pattern. Output goes into the trace file with log-level info.

If not aleady done, the list of available fonts will be downloaded from Google.

Types

type GoogleFontInfo

type GoogleFontInfo struct {
	font.Descriptor
	Version string            `json:"version"`
	Subsets []string          `json:"subsets"`
	Files   map[string]string `json:"files"`
}

GoogleFontInfo describes a font entry in the Google Font Service.

func FindGoogleFont

func FindGoogleFont(pattern string, style xfont.Style, weight xfont.Weight) ([]GoogleFontInfo, error)

FindGoogleFont scans the Google Font Service for fonts matching `pattern` and having a given style and weight.

Will include all fonts with a match-confidence greater than `font.LowConfidence`.

A prerequisite to looking for Google fonts is a valid API-key (refer to https://developers.google.com/fonts/docs/developer_api). It has to be configured either in the application setup or as an environment variable GOOGLE_API_KEY.

(Please refer to function `ResolveTypeCase`, too)

type ImagePromise

type ImagePromise interface {
	Image() (image.Image, error)
}

ImagePromise loads an image in the background. A call to `Image` will block until loading is completed.

func ResolveImage

func ResolveImage(name string, resolution string) ImagePromise

ResolveImage currently will only search for images packaged with the application.

type TypeCasePromise

type TypeCasePromise interface {
	TypeCase() (*font.TypeCase, error)
}

TypeCasePromise runs font location asynchronously in the background. A call to `TypeCase()` blocks until font loading is completed.

func ResolveTypeCase

func ResolveTypeCase(pattern string, style xfont.Style, weight xfont.Weight, size float64) TypeCasePromise

ResolveTypeCase resolves a font typecase with a given size. It searches for fonts in the following order:

▪︎ Fonts packaged with the application binary

▪︎ System-fonts

▪︎ Google Fonts service (https://fonts.google.com/)

ResolveTypeCase will try to match style and weight requirements closely, but will load a font variant anyway if it matches approximately. If, for example, a system contains a font with weight 300, which would be considered a "light" variant, but no variant with weight 400 (normal), it will load the 300-variant.

When looking for sytem-fonts, ResolveTypeCase will use an existing fontconfig (https://www.freedesktop.org/wiki/Software/fontconfig/) installation, if present. fontconfig has to be configured in the global application setup by pointing to the absolute path of the `fc-list` binary. If fontconfig isn't installed or configured, then this step will silently be skipped and a file system scan of the sytem's fonts-folders will be done. (See also function `FindLocalFont`).

A prerequisite to looking for Google fonts is a valid API-key (refer to https://developers.google.com/fonts/docs/developer_api). It has to be configured either in the application setup or as an environment variable GOOGLE_API_KEY. (See also function `FindGoogleFont`).

If no suitable font can be found, an application-wide fallback font will be returned.

Typecases are not returned synchronously, but rather as a promise of kind TypeCasePromise (async/await-pattern).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL