Skip to content

Commit

Permalink
feat(api): #453 Introduce Font
Browse files Browse the repository at this point in the history
  • Loading branch information
kashike committed Feb 4, 2022
1 parent 41d2a9d commit 29ef498
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 0 deletions.
81 changes: 81 additions & 0 deletions api/src/main/java/net/kyori/adventure/text/format/Font.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* This file is part of adventure, licensed under the MIT License.
*
* Copyright (c) 2017-2021 KyoriPowered
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package net.kyori.adventure.text.format;

import java.util.Objects;
import java.util.stream.Stream;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import net.kyori.examination.Examinable;
import net.kyori.examination.ExaminableProperty;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* A container around a {@link Component}'s {@link Component#font() font}.
*
* @since 4.10.0
*/
public interface Font extends Examinable, StyleBuilderApplicable {
/**
* Creates a {@code Font}.
*
* @param font the font
* @return a {@code Font}
* @since 4.10.0
*/
static @NotNull Font font(final @NotNull Key font) {
Objects.requireNonNull(font, "font");
return new FontImpl(font);
}

/**
* Fetches a {@link Key} from a {@code Font}.
*
* @param font the font
* @return a font key, or {@code null}
* @since 4.10.0
*/
static @Nullable Key unbox(final @Nullable Font font) {
return font != null ? font.font() : null;
}

/**
* Gets the font.
*
* @return the font
* @since 4.10.0
*/
@NotNull Key font();

@Override
default void styleApply(final Style.@NotNull Builder style) {
style.font(this.font());
}

@Override
default @NotNull Stream<? extends ExaminableProperty> examinableProperties() {
return Stream.of(ExaminableProperty.of("font", this.font()));
}
}
61 changes: 61 additions & 0 deletions api/src/main/java/net/kyori/adventure/text/format/FontImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* This file is part of adventure, licensed under the MIT License.
*
* Copyright (c) 2017-2021 KyoriPowered
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package net.kyori.adventure.text.format;

import java.util.Objects;
import net.kyori.adventure.internal.Internals;
import net.kyori.adventure.key.Key;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

final class FontImpl implements Font {
private final Key font;

FontImpl(final Key font) {
this.font = font;
}

@Override
public @NotNull Key font() {
return this.font;
}

@Override
public boolean equals(final @Nullable Object other) {
if (this == other) return true;
if (!(other instanceof Font)) return false;
final Font that = (Font) other;
return this.font.equals(that.font());
}

@Override
public int hashCode() {
return Objects.hash(this.font);
}

@Override
public String toString() {
return Internals.toString(this);
}
}
12 changes: 12 additions & 0 deletions api/src/main/java/net/kyori/adventure/text/format/StyleSetter.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ public interface StyleSetter<T extends StyleSetter<?>> {
*/
@NotNull T font(final @Nullable Key font);

/**
* Sets the font.
*
* @param font the font
* @return an object ({@code T})
* @since 4.10.0
* @sinceMinecraft 1.16
*/
default @NotNull T font(final @Nullable Font font) {
return this.font(Font.unbox(font));
}

/**
* Sets the color.
*
Expand Down

0 comments on commit 29ef498

Please sign in to comment.