|
1 | | - package org.cobalt.api.util.ui.helper |
2 | | - |
3 | | - import java.io.FileNotFoundException |
4 | | - import java.nio.ByteBuffer |
5 | | - import java.nio.ByteOrder |
6 | | - |
7 | | - /** |
8 | | - * Implementation from OdinFabric |
9 | | - * Original work: https://github.com/odtheking/OdinFabric |
10 | | - * |
11 | | - * @author OdinFabric |
12 | | - */ |
13 | | - class Font(val name: String, private val resourcePath: String) { |
14 | | - |
15 | | - private var cachedBytes: ByteArray? = null |
16 | | - |
17 | | - fun buffer(): ByteBuffer { |
18 | | - val bytes = cachedBytes ?: run { |
19 | | - val stream = this::class.java.getResourceAsStream(resourcePath) |
20 | | - ?: throw FileNotFoundException(resourcePath) |
21 | | - |
22 | | - stream.use { it.readBytes() }.also { cachedBytes = it } |
23 | | - } |
24 | | - |
25 | | - return ByteBuffer.allocateDirect(bytes.size) |
26 | | - .order(ByteOrder.nativeOrder()) |
27 | | - .put(bytes) |
28 | | - .flip() as ByteBuffer |
29 | | - } |
| 1 | +/* |
| 2 | + * BSD 3-Clause License |
| 3 | + * |
| 4 | + * Copyright (c) 2023-2025, odtheking |
| 5 | + * |
| 6 | + * Redistribution and use in source and binary forms, with or without |
| 7 | + * modification, are permitted provided that the following conditions are met: |
| 8 | + * |
| 9 | + * 1. Redistributions of source code must retain the above copyright notice, this |
| 10 | + * list of conditions and the following disclaimer. |
| 11 | + * |
| 12 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 | + * this list of conditions and the following disclaimer in the documentation |
| 14 | + * and/or other materials provided with the distribution. |
| 15 | + * |
| 16 | + * 3. Neither the name of the copyright holder nor the names of its |
| 17 | + * contributors may be used to endorse or promote products derived from |
| 18 | + * this software without specific prior written permission. |
| 19 | + * |
| 20 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 21 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 22 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 23 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 24 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 25 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 26 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 27 | + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 28 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | + * |
| 31 | + * Portions of this file are derived from OdinFabric |
| 32 | + * Copyright (c) odtheking |
| 33 | + * Licensed under BSD-3-Clause |
| 34 | + * |
| 35 | + * Modifications and additions: |
| 36 | + * Licensed under GPL-3.0 |
| 37 | + */ |
30 | 38 |
|
31 | | - override fun hashCode(): Int { |
32 | | - return name.hashCode() |
33 | | - } |
| 39 | +package org.cobalt.api.util.ui.helper |
| 40 | + |
| 41 | +import java.io.FileNotFoundException |
| 42 | +import java.nio.ByteBuffer |
| 43 | +import java.nio.ByteOrder |
| 44 | + |
| 45 | +class Font(val name: String, private val resourcePath: String) { |
| 46 | + |
| 47 | + private var cachedBytes: ByteArray? = null |
| 48 | + |
| 49 | + fun buffer(): ByteBuffer { |
| 50 | + val bytes = cachedBytes ?: run { |
| 51 | + val stream = this::class.java.getResourceAsStream(resourcePath) |
| 52 | + ?: throw FileNotFoundException(resourcePath) |
34 | 53 |
|
35 | | - override fun equals(other: Any?): Boolean { |
36 | | - return other is Font && name == other.name |
| 54 | + stream.use { it.readBytes() }.also { cachedBytes = it } |
37 | 55 | } |
38 | 56 |
|
| 57 | + return ByteBuffer.allocateDirect(bytes.size) |
| 58 | + .order(ByteOrder.nativeOrder()) |
| 59 | + .put(bytes) |
| 60 | + .flip() as ByteBuffer |
39 | 61 | } |
| 62 | + |
| 63 | + override fun hashCode(): Int { |
| 64 | + return name.hashCode() |
| 65 | + } |
| 66 | + |
| 67 | + override fun equals(other: Any?): Boolean { |
| 68 | + return other is Font && name == other.name |
| 69 | + } |
| 70 | + |
| 71 | +} |
0 commit comments