Makes FontData importable resource.

Adds multi-channel SDF font texture generation and rendering support.
Adds per-font oversampling support.
Adds FontData import plugins (for dynamic fonts, BMFonts and monospaced image fonts), font texture cache pre-generation and loading.
Adds BMFont binary format and outline support.
This commit is contained in:
bruvzg 2020-12-27 15:30:33 +02:00
parent 00268e37a0
commit 4c3f7d1290
130 changed files with 17847 additions and 6893 deletions

View File

@ -377,6 +377,11 @@ Comment: YUV2RGB
Copyright: 2008-2011, Robin Watts
License: BSD-2-clause
Files: ./thirdparty/msdfgen/
Comment: Multi-channel signed distance field generator
Copyright: 2016, Viktor Chlumsky
License: MIT
Files: ./thirdparty/nanosvg/
Comment: NanoSVG
Copyright: 2013-2014, Mikko Mononen

View File

@ -156,6 +156,7 @@ opts.Add(BoolVariable("builtin_certs", "Use the built-in SSL certificates bundle
opts.Add(BoolVariable("builtin_embree", "Use the built-in Embree library", True))
opts.Add(BoolVariable("builtin_enet", "Use the built-in ENet library", True))
opts.Add(BoolVariable("builtin_freetype", "Use the built-in FreeType library", True))
opts.Add(BoolVariable("builtin_msdfgen", "Use the built-in MSDFgen library", True))
opts.Add(BoolVariable("builtin_glslang", "Use the built-in glslang library", True))
opts.Add(BoolVariable("builtin_graphite", "Use the built-in Graphite library", True))
opts.Add(BoolVariable("builtin_harfbuzz", "Use the built-in HarfBuzz library", True))

View File

@ -106,6 +106,20 @@
Draws a [Mesh] in 2D, using the provided texture. See [MeshInstance2D] for related documentation.
</description>
</method>
<method name="draw_msdf_texture_rect_region">
<return type="void" />
<argument index="0" name="texture" type="Texture2D" />
<argument index="1" name="rect" type="Rect2" />
<argument index="2" name="src_rect" type="Rect2" />
<argument index="3" name="modulate" type="Color" default="Color(1, 1, 1, 1)" />
<argument index="4" name="outline" type="float" default="0.0" />
<argument index="5" name="pixel_range" type="float" default="4.0" />
<description>
Draws a textured rectangle region of the multi-channel signed distance field texture at a given position, optionally modulated by a color.
If [code]outline[/code] is positive, each alpha channel value of pixel in region is set to maximum value of true distance in the [code]outline[/code] radius.
Value of the [code]pixel_range[/code] should the same that was used during distance field texture generation.
</description>
</method>
<method name="draw_multiline">
<return type="void" />
<argument index="0" name="points" type="PackedVector2Array" />

View File

@ -70,6 +70,12 @@
Add font data source to the set.
</description>
</method>
<method name="clear_data">
<return type="void" />
<description>
Removes all font data sourcers for the set.
</description>
</method>
<method name="draw_char" qualifiers="const">
<return type="float" />
<argument index="0" name="canvas_item" type="RID" />
@ -151,6 +157,13 @@
Returns the number of font data sources.
</description>
</method>
<method name="get_data_rid" qualifiers="const">
<return type="RID" />
<argument index="0" name="idx" type="int" />
<description>
Returns TextServer RID of the font data resources.
</description>
</method>
<method name="get_descent" qualifiers="const">
<return type="float" />
<argument index="0" name="size" type="int" default="-1" />
@ -180,15 +193,18 @@
</method>
<method name="get_spacing" qualifiers="const">
<return type="int" />
<argument index="0" name="type" type="int" />
<argument index="0" name="spacing" type="int" enum="TextServer.SpacingType" />
<description>
Returns the spacing for the given [code]type[/code] (see [enum SpacingType]).
Returns the spacing for the given [code]type[/code] (see [enum TextServer.SpacingType]).
</description>
</method>
<method name="get_string_size" qualifiers="const">
<return type="Vector2" />
<argument index="0" name="text" type="String" />
<argument index="1" name="size" type="int" default="-1" />
<argument index="2" name="align" type="int" enum="HAlign" default="0" />
<argument index="3" name="width" type="float" default="-1" />
<argument index="4" name="flags" type="int" default="3" />
<description>
Returns the size of a bounding box of a string, taking kerning and advance into account.
[b]Note:[/b] Real height of the string is context-dependent and can be significantly different from the value returned by [method get_height].
@ -242,10 +258,10 @@
</method>
<method name="set_spacing">
<return type="void" />
<argument index="0" name="type" type="int" />
<argument index="0" name="spacing" type="int" enum="TextServer.SpacingType" />
<argument index="1" name="value" type="int" />
<description>
Sets the spacing for [code]type[/code] (see [enum SpacingType]) to [code]value[/code] in pixels (not relative to the font size).
Sets the spacing for [code]type[/code] (see [enum TextServer.SpacingType]) to [code]value[/code] in pixels (not relative to the font size).
</description>
</method>
<method name="update_changes">
@ -256,19 +272,19 @@
</method>
</methods>
<members>
<member name="extra_spacing_bottom" type="int" setter="set_spacing" getter="get_spacing" default="0">
<member name="base_size" type="int" setter="set_base_size" getter="get_base_size" default="16">
Default font size.
</member>
<member name="spacing_bottom" type="int" setter="set_spacing" getter="get_spacing" default="0">
Extra spacing at the bottom of the line in pixels.
</member>
<member name="extra_spacing_top" type="int" setter="set_spacing" getter="get_spacing" default="0">
<member name="spacing_top" type="int" setter="set_spacing" getter="get_spacing" default="0">
Extra spacing at the top of the line in pixels.
</member>
<member name="variation_coordinates" type="Dictionary" setter="set_variation_coordinates" getter="get_variation_coordinates" default="{}">
Default font [url=https://docs.microsoft.com/en-us/typography/opentype/spec/dvaraxisreg]variation coordinates[/url].
</member>
</members>
<constants>
<constant name="SPACING_TOP" value="0" enum="SpacingType">
Spacing at the top of the line.
</constant>
<constant name="SPACING_BOTTOM" value="1" enum="SpacingType">
Spacing at the bottom of the line.
</constant>
</constants>
</class>

View File

@ -1,122 +1,179 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="FontData" inherits="Resource" version="4.0">
<brief_description>
Font data source, file or memory buffer.
Font source data and prerendered glyph cache, imported from dynamic or bitmap font.
Supported font formats:
- Dynamic font importer: TrueType (.ttf), OpenType (.otf), WOFF (.woff), Type 1 (.pfb, .pfm).
- Bitmap font importer: AngelCode BMFont (.fnt, .font), text and binary (version 3) format variants.
- Monospace image font importer: All supported image formats.
</brief_description>
<description>
Built-in text servers support font data sources of the following formats:
- Bitmap fonts in the [url=https://www.angelcode.com/products/bmfont/]BMFont[/url] format. Handles [code].fnt, *.font[/code] fonts containing texture atlases. Non-scalable. Supports distance fields. Complex text shaping support is limited.
- Dynamic fonts using the [url=https://www.freetype.org/]FreeType[/url] and [url=https://github.com/silnrsi/graphite/]Graphite[/url] library for rasterization. Handles [code]*.ttf, *.otf[/code] fonts. Scalable. Doesn't support distance fields. Supports complex text shaping and OpenType features.
</description>
<tutorials>
</tutorials>
<methods>
<method name="bitmap_add_char">
<method name="clear_cache">
<return type="void" />
<argument index="0" name="char" type="int" />
<argument index="1" name="texture_idx" type="int" />
<argument index="2" name="rect" type="Rect2" />
<argument index="3" name="align" type="Vector2" />
<argument index="4" name="advance" type="float" />
<description>
Adds a character to the font, where [code]character[/code] is the Unicode value, [code]texture[/code] is the texture index, [code]rect[/code] is the region in the texture (in pixels!), [code]align[/code] is the (optional) alignment for the character and [code]advance[/code] is the (optional) advance.
Removes all font cache entries.
</description>
</method>
<method name="bitmap_add_kerning_pair">
<method name="clear_glyphs">
<return type="void" />
<argument index="0" name="A" type="int" />
<argument index="1" name="B" type="int" />
<argument index="2" name="kerning" type="int" />
<argument index="0" name="cache_index" type="int" />
<argument index="1" name="size" type="Vector2i" />
<description>
Adds a kerning pair to the bitmap font as a difference. Kerning pairs are special cases where a typeface advance is determined by the next character.
Removes all rendered glyphs information from the cache entry. Note: This function will not remove textures associated with the glyphs, use [method remove_texture] to remove them manually.
</description>
</method>
<method name="bitmap_add_texture">
<method name="clear_kerning_map">
<return type="void" />
<argument index="0" name="texture" type="Texture" />
<description>
Adds a texture to the bitmap font.
</description>
</method>
<method name="draw_glyph" qualifiers="const">
<return type="Vector2" />
<argument index="0" name="canvas" type="RID" />
<argument index="0" name="cache_index" type="int" />
<argument index="1" name="size" type="int" />
<argument index="2" name="pos" type="Vector2" />
<argument index="3" name="index" type="int" />
<argument index="4" name="color" type="Color" default="Color(1, 1, 1, 1)" />
<description>
Draws single glyph into a canvas item at the position, using [code]font[/code] at the size [code]size[/code].
Returns advance of the glyph for horizontal and vertical layouts.
Note: Glyph index is bound to the font data, use only glyphs indices returned by [method TextServer.shaped_text_get_glyphs] or [method get_glyph_index] for this font data.
Removes all kerning overrides.
</description>
</method>
<method name="draw_glyph_outline" qualifiers="const">
<return type="Vector2" />
<argument index="0" name="canvas" type="RID" />
<argument index="1" name="size" type="int" />
<argument index="2" name="outline_size" type="int" />
<argument index="3" name="pos" type="Vector2" />
<argument index="4" name="index" type="int" />
<argument index="5" name="color" type="Color" default="Color(1, 1, 1, 1)" />
<method name="clear_size_cache">
<return type="void" />
<argument index="0" name="cache_index" type="int" />
<description>
Draws single glyph outline of size [code]outline_size[/code] into a canvas item at the position, using [code]font[/code] at the size [code]size[/code]. If outline drawing is not supported, nothing is drawn.
Returns advance of the glyph for horizontal and vertical layouts (regardless of outline drawing support).
Note: Glyph index is bound to the font data, use only glyphs indices returned by [method TextServer.shaped_text_get_glyphs] or [method get_glyph_index] for this font data.
Removes all font sizes from the cache entry
</description>
</method>
<method name="clear_textures">
<return type="void" />
<argument index="0" name="cache_index" type="int" />
<argument index="1" name="size" type="Vector2i" />
<description>
Removes all textures from font cache entry. Note: This function will not remove glyphs associated with the texture, use [method remove_glyph] to remove them manually.
</description>
</method>
<method name="find_cache" qualifiers="const">
<return type="RID" />
<argument index="0" name="variation_coordinates" type="Dictionary" />
<description>
Returns existing or creates a new font cache entry for the specified variation coordinates.
</description>
</method>
<method name="get_ascent" qualifiers="const">
<return type="float" />
<argument index="0" name="size" type="int" />
<argument index="0" name="cache_index" type="int" />
<argument index="1" name="size" type="int" />
<description>
Returns the font ascent (number of pixels above the baseline).
</description>
</method>
<method name="get_base_size" qualifiers="const">
<return type="float" />
<method name="get_cache_count" qualifiers="const">
<return type="int" />
<description>
Returns the base size of the font (the only size supported for non-scalable fonts, meaningless for scalable fonts).
Returns number of the font cache entries.
</description>
</method>
<method name="get_cache_rid" qualifiers="const">
<return type="RID" />
<argument index="0" name="cache_index" type="int" />
<description>
Returns text server font cache entry resource id.
</description>
</method>
<method name="get_data" qualifiers="const">
<return type="PackedByteArray" />
<description>
Returns contents of the dynamic font source file.
</description>
</method>
<method name="get_descent" qualifiers="const">
<return type="float" />
<argument index="0" name="size" type="int" />
<argument index="0" name="cache_index" type="int" />
<argument index="1" name="size" type="int" />
<description>
Returns the font descent (number of pixels below the baseline).
Returns font descent (number of pixels below the baseline).
</description>
</method>
<method name="get_glyph_advance" qualifiers="const">
<return type="Vector2" />
<argument index="0" name="index" type="int" />
<argument index="0" name="cache_index" type="int" />
<argument index="1" name="size" type="int" />
<argument index="2" name="glyph" type="int" />
<description>
Returns advance of the glyph for horizontal and vertical layouts.
Note: Glyph index is bound to the font data, use only glyphs indices returned by [method TextServer.shaped_text_get_glyphs] or [method get_glyph_index] for this font data.
Returns glyph advance (offset of the next glyph). Note: advance for glyphs outlines is the same as the base glyph advance and is not saved.
</description>
</method>
<method name="get_glyph_index" qualifiers="const">
<return type="int" />
<argument index="0" name="char" type="int" />
<argument index="1" name="variation_selector" type="int" default="0" />
<argument index="1" name="variation_selector" type="int" />
<argument index="2" name="arg2" type="int" />
<description>
Return the glyph index of a [code]char[/code], optionally modified by the [code]variation_selector[/code].
Returns the glyph index of a [code]char[/code], optionally modified by the [code]variation_selector[/code].
</description>
</method>
<method name="get_glyph_kerning" qualifiers="const">
<method name="get_glyph_list" qualifiers="const">
<return type="Array" />
<argument index="0" name="cache_index" type="int" />
<argument index="1" name="size" type="Vector2i" />
<description>
Returns list of rendered glyphs in the cache entry.
</description>
</method>
<method name="get_glyph_offset" qualifiers="const">
<return type="Vector2" />
<argument index="0" name="index_a" type="int" />
<argument index="1" name="index_b" type="int" />
<argument index="2" name="size" type="int" />
<argument index="0" name="cache_index" type="int" />
<argument index="1" name="size" type="Vector2i" />
<argument index="2" name="glyph" type="int" />
<description>
Returns a kerning of the pair of glyphs for horizontal and vertical layouts.
Note: Glyph index is bound to the font data, use only glyphs indices returned by [method TextServer.shaped_text_get_glyphs] or [method get_glyph_index] for this font data.
Returns glyph offset from the baseline.
</description>
</method>
<method name="get_height" qualifiers="const">
<return type="float" />
<argument index="0" name="size" type="int" />
<method name="get_glyph_size" qualifiers="const">
<return type="Vector2" />
<argument index="0" name="cache_index" type="int" />
<argument index="1" name="size" type="Vector2i" />
<argument index="2" name="glyph" type="int" />
<description>
Returns the total font height (ascent plus descent) in pixels.
Returns glyph size.
</description>
</method>
<method name="get_glyph_texture_idx" qualifiers="const">
<return type="int" />
<argument index="0" name="cache_index" type="int" />
<argument index="1" name="size" type="Vector2i" />
<argument index="2" name="glyph" type="int" />
<description>
Returns index of the cache texture containing the glyph.
</description>
</method>
<method name="get_glyph_uv_rect" qualifiers="const">
<return type="Rect2" />
<argument index="0" name="cache_index" type="int" />
<argument index="1" name="size" type="Vector2i" />
<argument index="2" name="glyph" type="int" />
<description>
Returns rectangle in the cache texture containing the glyph.
</description>
</method>
<method name="get_hinting" qualifiers="const">
<return type="int" enum="TextServer.Hinting" />
<description>
Returns the font hinting mode. Used by dynamic fonts only.
</description>
</method>
<method name="get_kerning" qualifiers="const">
<return type="Vector2" />
<argument index="0" name="cache_index" type="int" />
<argument index="1" name="size" type="int" />
<argument index="2" name="glyph_pair" type="Vector2i" />
<description>
Returns kerning for the pair of glyphs.
</description>
</method>
<method name="get_kerning_list" qualifiers="const">
<return type="Array" />
<argument index="0" name="cache_index" type="int" />
<argument index="1" name="size" type="int" />
<description>
Returns list of the kerning overrides.
</description>
</method>
<method name="get_language_support_override" qualifiers="const">
@ -132,6 +189,32 @@
Returns list of language support overrides.
</description>
</method>
<method name="get_msdf_pixel_range" qualifiers="const">
<return type="int" />
<description>
Returns the width of the range around the shape between the minimum and maximum representable signed distance.
</description>
</method>
<method name="get_msdf_size" qualifiers="const">
<return type="int" />
<description>
Returns source font size used to generate MSDF textures.
</description>
</method>
<method name="get_oversampling" qualifiers="const">
<return type="float" />
<description>
Returns font oversampling factor, if set to [code]0.0[/code] global oversampling factor is used instead. Used by dynamic fonts only.
</description>
</method>
<method name="get_scale" qualifiers="const">
<return type="float" />
<argument index="0" name="cache_index" type="int" />
<argument index="1" name="size" type="int" />
<description>
Returns scaling factor of the color bitmap font.
</description>
</method>
<method name="get_script_support_override" qualifiers="const">
<return type="bool" />
<argument index="0" name="script" type="String" />
@ -145,11 +228,20 @@
Returns list of script support overrides.
</description>
</method>
<method name="get_size_cache_list" qualifiers="const">
<return type="Array" />
<argument index="0" name="cache_index" type="int" />
<description>
Return list of the font sizes in the cache. Each size is [code]Vector2i[/code] with font size and outline size.
</description>
</method>
<method name="get_spacing" qualifiers="const">
<return type="int" />
<argument index="0" name="type" type="int" />
<argument index="0" name="cache_index" type="int" />
<argument index="1" name="size" type="int" />
<argument index="2" name="arg2" type="int" enum="TextServer.SpacingType" />
<description>
Returns the spacing for the given [code]type[/code] (see [enum SpacingType]).
Returns extra spacing added between glyphs in pixels.
</description>
</method>
<method name="get_supported_chars" qualifiers="const">
@ -158,34 +250,68 @@
Returns a string containing all the characters available in the font.
</description>
</method>
<method name="get_underline_position" qualifiers="const">
<return type="float" />
<argument index="0" name="size" type="int" />
<method name="get_supported_feature_list" qualifiers="const">
<return type="Dictionary" />
<description>
Returns underline offset (number of pixels below the baseline).
Returns list of OpenType features supported by font.
</description>
</method>
<method name="get_underline_thickness" qualifiers="const">
<return type="float" />
<argument index="0" name="size" type="int" />
<description>
Returns underline thickness in pixels.
</description>
</method>
<method name="get_variation" qualifiers="const">
<return type="float" />
<argument index="0" name="tag" type="String" />
<description>
Returns variation coordinate [code]tag[/code].
</description>
</method>
<method name="get_variation_list" qualifiers="const">
<method name="get_supported_variation_list" qualifiers="const">
<return type="Dictionary" />
<description>
Returns list of supported [url=https://docs.microsoft.com/en-us/typography/opentype/spec/dvaraxisreg]variation coordinates[/url], each coordinate is returned as [code]tag: Vector3i(min_value,max_value,default_value)[/code].
Font variations allow for continuous change of glyph characteristics along some given design axis, such as weight, width or slant.
</description>
</method>
<method name="get_texture_count" qualifiers="const">
<return type="int" />
<argument index="0" name="cache_index" type="int" />
<argument index="1" name="size" type="Vector2i" />
<description>
Returns number of textures used by font cache entry.
</description>
</method>
<method name="get_texture_image" qualifiers="const">
<return type="Image" />
<argument index="0" name="cache_index" type="int" />
<argument index="1" name="size" type="Vector2i" />
<argument index="2" name="texture_index" type="int" />
<description>
Returns a copy of the font cache texture image.
</description>
</method>
<method name="get_texture_offsets" qualifiers="const">
<return type="PackedInt32Array" />
<argument index="0" name="cache_index" type="int" />
<argument index="1" name="size" type="Vector2i" />
<argument index="2" name="texture_index" type="int" />
<description>
Returns a copy of the array containing the first free pixel in the each column of texture. Should be the same size as texture width or empty.
</description>
</method>
<method name="get_underline_position" qualifiers="const">
<return type="float" />
<argument index="0" name="cache_index" type="int" />
<argument index="1" name="size" type="int" />
<description>
Returns pixel offset of the underline below the baseline.
</description>
</method>
<method name="get_underline_thickness" qualifiers="const">
<return type="float" />
<argument index="0" name="cache_index" type="int" />
<argument index="1" name="size" type="int" />
<description>
Returns thickness of the underline in pixels.
</description>
</method>
<method name="get_variation_coordinates" qualifiers="const">
<return type="Dictionary" />
<argument index="0" name="cache_index" type="int" />
<description>
Returns variation coordinates for the specified font cache entry. See [method get_supported_variation_list] for more info.
</description>
</method>
<method name="has_char" qualifiers="const">
<return type="bool" />
<argument index="0" name="char" type="int" />
@ -193,10 +319,16 @@
Return [code]true[/code] if a Unicode [code]char[/code] is available in the font.
</description>
</method>
<method name="has_outline" qualifiers="const">
<method name="is_antialiased" qualifiers="const">
<return type="bool" />
<description>
Returns [code]true[/code], if font supports drawing glyph outlines.
Returns [code]true[/code] if font 8-bit anitialiased glyph rendering is supported and enabled.
</description>
</method>
<method name="is_force_autohinter" qualifiers="const">
<return type="bool" />
<description>
Returns [code]true[/code] if auto-hinting is supported and preffered over font built-in hinting. Used by dynamic fonts only.
</description>
</method>
<method name="is_language_supported" qualifiers="const">
@ -206,6 +338,12 @@
Returns [code]true[/code], if font supports given language ([url=https://en.wikipedia.org/wiki/ISO_639-1]ISO 639[/url] code).
</description>
</method>
<method name="is_multichannel_signed_distance_field" qualifiers="const">
<return type="bool" />
<description>
Returns [code]true[/code] if glyphs of all sizes are rendered using single multichannel signed distance field generated from the dynamic font vector data.
</description>
</method>
<method name="is_script_supported" qualifiers="const">
<return type="bool" />
<argument index="0" name="script" type="String" />
@ -213,32 +351,29 @@
Returns [code]true[/code], if font supports given script ([url=https://en.wikipedia.org/wiki/ISO_15924]ISO 15924[/url] code).
</description>
</method>
<method name="load_memory">
<method name="remove_cache">
<return type="void" />
<argument index="0" name="data" type="PackedByteArray" />
<argument index="1" name="type" type="String" />
<argument index="2" name="base_size" type="int" default="16" />
<argument index="0" name="cache_index" type="int" />
<description>
Creates new font from the data in memory.
Note: For non-scalable fonts [code]base_size[/code] is ignored, use [method get_base_size] to check actual font size.
Removes specified font cache entry.
</description>
</method>
<method name="load_resource">
<method name="remove_glyph">
<return type="void" />
<argument index="0" name="filename" type="String" />
<argument index="1" name="base_size" type="int" default="16" />
<argument index="0" name="cache_index" type="int" />
<argument index="1" name="size" type="Vector2i" />
<argument index="2" name="glyph" type="int" />
<description>
Creates new font from the file.
Note: For non-scalable fonts [code]base_size[/code] is ignored, use [method get_base_size] to check actual font size.
Removes specified rendered glyph information from the cache entry. Note: This function will not remove textures associated with the glyphs, use [method remove_texture] to remove them manually.
</description>
</method>
<method name="new_bitmap">
<method name="remove_kerning">
<return type="void" />
<argument index="0" name="height" type="float" />
<argument index="1" name="ascent" type="float" />
<argument index="2" name="base_size" type="int" />
<argument index="0" name="cache_index" type="int" />
<argument index="1" name="size" type="int" />
<argument index="2" name="glyph_pair" type="Vector2i" />
<description>
Creates new, empty bitmap font.
Removes kerning override for the pair of glyphs.
</description>
</method>
<method name="remove_language_support_override">
@ -255,6 +390,148 @@
Removes script support override.
</description>
</method>
<method name="remove_size_cache">
<return type="void" />
<argument index="0" name="cache_index" type="int" />
<argument index="1" name="size" type="Vector2i" />
<description>
Removes specified font size from the cache entry.
</description>
</method>
<method name="remove_texture">
<return type="void" />
<argument index="0" name="cache_index" type="int" />
<argument index="1" name="size" type="Vector2i" />
<argument index="2" name="texture_index" type="int" />
<description>
Removes specified texture from font cache entry. Note: This function will not remove glyphs associated with the texture, remove them manually, using [method remove_glyph].
</description>
</method>
<method name="render_glyph">
<return type="void" />
<argument index="0" name="cache_index" type="int" />
<argument index="1" name="size" type="Vector2i" />
<argument index="2" name="index" type="int" />
<description>
Renders specified glyph the the font cache texture.
</description>
</method>
<method name="render_range">
<return type="void" />
<argument index="0" name="cache_index" type="int" />
<argument index="1" name="size" type="Vector2i" />
<argument index="2" name="start" type="int" />
<argument index="3" name="end" type="int" />
<description>
Renders the range of characters to the font cache texture.
</description>
</method>
<method name="set_antialiased">
<return type="void" />
<argument index="0" name="antialiased" type="bool" />
<description>
If set to [code]true[/code], 8-bit antialiased glyph rendering is used, otherwise 1-bit rendering is used. Used by dynamic fonts only.
</description>
</method>
<method name="set_ascent">
<return type="void" />
<argument index="0" name="cache_index" type="int" />
<argument index="1" name="size" type="int" />
<argument index="2" name="ascent" type="float" />
<description>
Sets the font ascent (number of pixels above the baseline).
</description>
</method>
<method name="set_data">
<return type="void" />
<argument index="0" name="data" type="PackedByteArray" />
<description>
Sets font source data, e.g contents of the dynamic font source file.
</description>
</method>
<method name="set_descent">
<return type="void" />
<argument index="0" name="cache_index" type="int" />
<argument index="1" name="size" type="int" />
<argument index="2" name="descent" type="float" />
<description>
Sets the font descent (number of pixels below the baseline).
</description>
</method>
<method name="set_force_autohinter">
<return type="void" />
<argument index="0" name="force_autohinter" type="bool" />
<description>
If set to [code]true[/code] auto-hinting is preffered over font built-in hinting.
</description>
</method>
<method name="set_glyph_advance">
<return type="void" />
<argument index="0" name="cache_index" type="int" />
<argument index="1" name="size" type="int" />
<argument index="2" name="glyph" type="int" />
<argument index="3" name="advance" type="Vector2" />
<description>
Sets glyph advance (offset of the next glyph). Note: advance for glyphs outlines is the same as the base glyph advance and is not saved.
</description>
</method>
<method name="set_glyph_offset">
<return type="void" />
<argument index="0" name="cache_index" type="int" />
<argument index="1" name="size" type="Vector2i" />
<argument index="2" name="glyph" type="int" />
<argument index="3" name="offset" type="Vector2" />
<description>
Sets glyph offset from the baseline.
</description>
</method>
<method name="set_glyph_size">
<return type="void" />
<argument index="0" name="cache_index" type="int" />
<argument index="1" name="size" type="Vector2i" />
<argument index="2" name="glyph" type="int" />
<argument index="3" name="gl_size" type="Vector2" />
<description>
Sets glyph size.
</description>
</method>
<method name="set_glyph_texture_idx">
<return type="void" />
<argument index="0" name="cache_index" type="int" />
<argument index="1" name="size" type="Vector2i" />
<argument index="2" name="glyph" type="int" />
<argument index="3" name="texture_idx" type="int" />
<description>
Sets index of the cache texture containing the glyph.
</description>
</method>
<method name="set_glyph_uv_rect">
<return type="void" />
<argument index="0" name="cache_index" type="int" />
<argument index="1" name="size" type="Vector2i" />
<argument index="2" name="glyph" type="int" />
<argument index="3" name="uv_rect" type="Rect2" />
<description>
Sets rectangle in the cache texture containing the glyph.
</description>
</method>
<method name="set_hinting">
<return type="void" />
<argument index="0" name="hinting" type="int" enum="TextServer.Hinting" />
<description>
Sets font hinting mode. Used by dynamic fonts only.
</description>
</method>
<method name="set_kerning">
<return type="void" />
<argument index="0" name="cache_index" type="int" />
<argument index="1" name="size" type="int" />
<argument index="2" name="glyph_pair" type="Vector2i" />
<argument index="3" name="kerning" type="Vector2" />
<description>
Sets kerning for the pair of glyphs.
</description>
</method>
<method name="set_language_support_override">
<return type="void" />
<argument index="0" name="language" type="String" />
@ -263,6 +540,43 @@
Adds override for [method is_language_supported].
</description>
</method>
<method name="set_msdf_pixel_range">
<return type="void" />
<argument index="0" name="msdf_pixel_range" type="int" />
<description>
Sets the width of the range around the shape between the minimum and maximum representable signed distance.
</description>
</method>
<method name="set_msdf_size">
<return type="void" />
<argument index="0" name="msdf_size" type="int" />
<description>
Sets source font size used to generate MSDF textures.
</description>
</method>
<method name="set_multichannel_signed_distance_field">
<return type="void" />
<argument index="0" name="msdf" type="bool" />
<description>
If set to [code]true[/code], glyphs of all sizes are rendered using single multichannel signed distance field generated from the dynamic font vector data.
</description>
</method>
<method name="set_oversampling">
<return type="void" />
<argument index="0" name="oversampling" type="float" />
<description>
Sets font oversampling factor, if set to [code]0.0[/code] global oversampling factor is used instead. Used by dynamic fonts only.
</description>
</method>
<method name="set_scale">
<return type="void" />
<argument index="0" name="cache_index" type="int" />
<argument index="1" name="size" type="int" />
<argument index="2" name="scale" type="float" />
<description>
Sets scaling factor of the color bitmap font.
</description>
</method>
<method name="set_script_support_override">
<return type="void" />
<argument index="0" name="script" type="String" />
@ -273,52 +587,61 @@
</method>
<method name="set_spacing">
<return type="void" />
<argument index="0" name="type" type="int" />
<argument index="1" name="value" type="int" />
<argument index="0" name="cache_index" type="int" />
<argument index="1" name="size" type="int" />
<argument index="2" name="spacing" type="int" enum="TextServer.SpacingType" />
<argument index="3" name="arg3" type="int" />
<description>
Sets the spacing for [code]type[/code] (see [enum SpacingType]) to [code]value[/code] in pixels (not relative to the font size).
Sets extra spacing added between glyphs in pixels.
</description>
</method>
<method name="set_variation">
<method name="set_texture_image">
<return type="void" />
<argument index="0" name="tag" type="String" />
<argument index="1" name="value" type="float" />
<argument index="0" name="cache_index" type="int" />
<argument index="1" name="size" type="Vector2i" />
<argument index="2" name="texture_index" type="int" />
<argument index="3" name="image" type="Image" />
<description>
Sets variation coordinate [code]tag[/code].
Sets font cache texture image.
</description>
</method>
<method name="set_texture_offsets">
<return type="void" />
<argument index="0" name="cache_index" type="int" />
<argument index="1" name="size" type="Vector2i" />
<argument index="2" name="texture_index" type="int" />
<argument index="3" name="offset" type="PackedInt32Array" />
<description>
Sets array containing the first free pixel in the each column of texture. Should be the same size as texture width or empty (for the fonts without dynamic glyph generation support).
</description>
</method>
<method name="set_underline_position">
<return type="void" />
<argument index="0" name="cache_index" type="int" />
<argument index="1" name="size" type="int" />
<argument index="2" name="underline_position" type="float" />
<description>
Sets pixel offset of the underline below the baseline.
</description>
</method>
<method name="set_underline_thickness">
<return type="void" />
<argument index="0" name="cache_index" type="int" />
<argument index="1" name="size" type="int" />
<argument index="2" name="underline_thickness" type="float" />
<description>
Sets thickness of the underline in pixels.
</description>
</method>
<method name="set_variation_coordinates">
<return type="void" />
<argument index="0" name="cache_index" type="int" />
<argument index="1" name="variation_coordinates" type="Dictionary" />
<description>
Sets variation coordinates for the specified font cache entry. See [method get_supported_variation_list] for more info.
</description>
</method>
</methods>
<members>
<member name="antialiased" type="bool" setter="set_antialiased" getter="get_antialiased" default="false">
If [code]true[/code], the font is rendered with anti-aliasing.
</member>
<member name="data_path" type="String" setter="set_data_path" getter="get_data_path" default="&quot;&quot;">
The path to the font data file. If font data was loaded from memory location is set to [code]"(Memory)"[/code].
</member>
<member name="distance_field_hint" type="bool" setter="set_distance_field_hint" getter="get_distance_field_hint" default="false">
If [code]true[/code], distance field hint is enabled.
</member>
<member name="extra_spacing_glyph" type="int" setter="set_spacing" getter="get_spacing" default="0">
Extra spacing for each glyph in pixels.
This can be a negative number to make the distance between glyphs smaller.
</member>
<member name="extra_spacing_space" type="int" setter="set_spacing" getter="get_spacing" default="0">
Extra spacing for the space character in pixels.
This can be a negative number to make the distance between words smaller.
</member>
<member name="force_autohinter" type="bool" setter="set_force_autohinter" getter="get_force_autohinter" default="false">
If [code]true[/code], default autohinter is used for font hinting.
</member>
<member name="hinting" type="int" setter="set_hinting" getter="get_hinting" enum="TextServer.Hinting" default="0">
The font hinting mode used by FreeType. See [enum TextServer.Hinting] for options.
</member>
</members>
<constants>
<constant name="SPACING_GLYPH" value="0" enum="SpacingType">
Spacing for each glyph.
</constant>
<constant name="SPACING_SPACE" value="1" enum="SpacingType">
Spacing for the space character.
</constant>
</constants>
</class>

View File

@ -220,13 +220,13 @@
<method name="get_spacing_bottom" qualifiers="const">
<return type="int" />
<description>
Returns extra spacing at the bottom of the line. See [member Font.extra_spacing_bottom].
Returns extra spacing at the bottom of the line. See [member Font.spacing_bottom].
</description>
</method>
<method name="get_spacing_top" qualifiers="const">
<return type="int" />
<description>
Returns extra spacing at the top of the line. See [member Font.extra_spacing_top].
Returns extra spacing at the top of the line. See [member Font.spacing_top].
</description>
</method>
<method name="hit_test" qualifiers="const">

View File

@ -9,42 +9,10 @@
<tutorials>
</tutorials>
<methods>
<method name="create_font_bitmap">
<method name="create_font">
<return type="RID" />
<argument index="0" name="height" type="float" />
<argument index="1" name="ascent" type="float" />
<argument index="2" name="base_size" type="int" />
<description>
Creates new, empty bitmap font. To free the resulting font, use [method free_rid] method.
</description>
</method>
<method name="create_font_memory">
<return type="RID" />
<argument index="0" name="data" type="PackedByteArray" />
<argument index="1" name="type" type="String" />
<argument index="2" name="base_size" type="int" default="16" />
<description>
Creates new font from the data in memory. To free the resulting font, use [method free_rid] method.
Note: For non-scalable fonts [code]base_size[/code] is ignored, use [method font_get_base_size] to check actual font size.
</description>
</method>
<method name="create_font_resource">
<return type="RID" />
<argument index="0" name="filename" type="String" />
<argument index="1" name="base_size" type="int" default="16" />
<description>
Creates new font from the file. To free the resulting font, use [method free_rid] method.
Note: For non-scalable fonts [code]base_size[/code] is ignored, use [method font_get_base_size] to check actual font size.
</description>
</method>
<method name="create_font_system">
<return type="RID" />
<argument index="0" name="name" type="String" />
<argument index="1" name="base_size" type="int" default="16" />
<description>
Creates new font from the system font. To free the resulting font, use [method free_rid] method.
Note: This method is supported by servers with the [code]FEATURE_FONT_SYSTEM[/code] feature.
Note: For non-scalable fonts [code]base_size[/code] is ignored, use [method font_get_base_size] to check actual font size.
Creates new, empty font cache entry resource. To free the resulting resourec, use [method free_rid] method.
</description>
</method>
<method name="create_shaped_text">
@ -68,52 +36,53 @@
Draws box displaying character hexadecimal code. Used for replacing missing characters.
</description>
</method>
<method name="font_bitmap_add_char">
<method name="font_clear_glyphs">
<return type="void" />
<argument index="0" name="font" type="RID" />
<argument index="1" name="char" type="int" />
<argument index="2" name="texture_idx" type="int" />
<argument index="3" name="rect" type="Rect2" />
<argument index="4" name="align" type="Vector2" />
<argument index="5" name="advance" type="float" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="size" type="Vector2i" />
<description>
Adds a character to the font, where [code]character[/code] is the Unicode value, [code]texture[/code] is the texture index, [code]rect[/code] is the region in the texture (in pixels!), [code]align[/code] is the (optional) alignment for the character and [code]advance[/code] is the (optional) advance.
Removes all rendered glyphs information from the cache entry. Note: This function will not remove textures associated with the glyphs, use [method font_remove_texture] to remove them manually.
</description>
</method>
<method name="font_bitmap_add_kerning_pair">
<method name="font_clear_kerning_map">
<return type="void" />
<argument index="0" name="font" type="RID" />
<argument index="1" name="A" type="int" />
<argument index="2" name="B" type="int" />
<argument index="3" name="kerning" type="int" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="size" type="int" />
<description>
Adds a kerning pair to the bitmap font as a difference. Kerning pairs are special cases where a typeface advance is determined by the next character.
Removes all kerning overrides.
</description>
</method>
<method name="font_bitmap_add_texture">
<method name="font_clear_size_cache">
<return type="void" />
<argument index="0" name="font" type="RID" />
<argument index="1" name="texture" type="Texture" />
<argument index="0" name="font_rid" type="RID" />
<description>
Adds a texture to the bitmap font.
Removes all font sizes from the cache entry
</description>
</method>
<method name="font_clear_textures">
<return type="void" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="size" type="Vector2i" />
<description>
Removes all textures from font cache entry. Note: This function will not remove glyphs associated with the texture, use [method font_remove_glyph] to remove them manually.
</description>
</method>
<method name="font_draw_glyph" qualifiers="const">
<return type="Vector2" />
<argument index="0" name="font" type="RID" />
<return type="void" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="canvas" type="RID" />
<argument index="2" name="size" type="int" />
<argument index="3" name="pos" type="Vector2" />
<argument index="4" name="index" type="int" />
<argument index="5" name="color" type="Color" default="Color(1, 1, 1, 1)" />
<description>
Draws single glyph into a canvas item at the position, using [code]font[/code] at the size [code]size[/code].
Draws single glyph into a canvas item at the position, using [code]font_rid[/code] at the size [code]size[/code].
Note: Glyph index is specific to the font, use glyphs indices returned by [method shaped_text_get_glyphs] or [method font_get_glyph_index].
</description>
</method>
<method name="font_draw_glyph_outline" qualifiers="const">
<return type="Vector2" />
<argument index="0" name="font" type="RID" />
<return type="void" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="canvas" type="RID" />
<argument index="2" name="size" type="int" />
<argument index="3" name="outline_size" type="int" />
@ -121,68 +90,46 @@
<argument index="5" name="index" type="int" />
<argument index="6" name="color" type="Color" default="Color(1, 1, 1, 1)" />
<description>
Draws single glyph outline of size [code]outline_size[/code] into a canvas item at the position, using [code]font[/code] at the size [code]size[/code].
Draws single glyph outline of size [code]outline_size[/code] into a canvas item at the position, using [code]font_rid[/code] at the size [code]size[/code].
Note: Glyph index is specific to the font, use glyphs indices returned by [method shaped_text_get_glyphs] or [method font_get_glyph_index].
</description>
</method>
<method name="font_get_antialiased" qualifiers="const">
<return type="bool" />
<argument index="0" name="font" type="RID" />
<description>
Returns [code]true[/code], if font anti-aliasing is supported and enabled.
</description>
</method>
<method name="font_get_ascent" qualifiers="const">
<return type="float" />
<argument index="0" name="font" type="RID" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="size" type="int" />
<description>
Returns the font ascent (number of pixels above the baseline).
</description>
</method>
<method name="font_get_base_size" qualifiers="const">
<return type="float" />
<argument index="0" name="font" type="RID" />
<description>
Returns the default size of the font.
</description>
</method>
<method name="font_get_descent" qualifiers="const">
<return type="float" />
<argument index="0" name="font" type="RID" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="size" type="int" />
<description>
Returns the font descent (number of pixels below the baseline).
</description>
</method>
<method name="font_get_distance_field_hint" qualifiers="const">
<return type="bool" />
<argument index="0" name="font" type="RID" />
<method name="font_get_fixed_size" qualifiers="const">
<return type="int" />
<argument index="0" name="font_rid" type="RID" />
<description>
Returns [code]true[/code], if distance field hint is enabled.
Returns bitmap font fixed size.
</description>
</method>
<method name="font_get_feature_list" qualifiers="const">
<return type="Dictionary" />
<argument index="0" name="font" type="RID" />
<method name="font_get_global_oversampling" qualifiers="const">
<return type="float" />
<description>
Returns list of OpenType features supported by font.
</description>
</method>
<method name="font_get_force_autohinter" qualifiers="const">
<return type="bool" />
<argument index="0" name="font" type="RID" />
<description>
Returns [code]true[/code], if autohinter is supported and enabled.
Returns the font oversampling factor, shared by all fonts in the TextServer.
</description>
</method>
<method name="font_get_glyph_advance" qualifiers="const">
<return type="Vector2" />
<argument index="0" name="font" type="RID" />
<argument index="1" name="index" type="int" />
<argument index="2" name="size" type="int" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="size" type="int" />
<argument index="2" name="glyph" type="int" />
<description>
Returns advance of the glyph.
Returns glyph advance (offset of the next glyph). Note: advance for glyphs outlines is the same as the base glyph advance and is not saved.
</description>
</method>
<method name="font_get_glyph_contours" qualifiers="const">
@ -191,7 +138,7 @@
<argument index="1" name="size" type="int" />
<argument index="2" name="index" type="int" />
<description>
Returns outline contours of the glyph in a Dictionary.
Returns outline contours of the glyph as a [code]Dictionary[/code] with the following contents:
[code]points[/code] - [PackedVector3Array], containing outline points. [code]x[/code] and [code]y[/code] are point coordinates. [code]z[/code] is the type of the point, using the [enum ContourPointTag] values.
[code]contours[/code] - [PackedInt32Array], containing indices the end points of each contour.
[code]orientation[/code] - [bool], contour orientation. If [code]true[/code], clockwise contours must be filled.
@ -199,41 +146,85 @@
</method>
<method name="font_get_glyph_index" qualifiers="const">
<return type="int" />
<argument index="0" name="font" type="RID" />
<argument index="1" name="char" type="int" />
<argument index="2" name="variation_selector" type="int" default="0" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="size" type="int" />
<argument index="2" name="char" type="int" />
<argument index="3" name="variation_selector" type="int" />
<description>
Returns the glyph index of a [code]char[/code], optionally modified by the [code]variation_selector[/code].
</description>
</method>
<method name="font_get_glyph_kerning" qualifiers="const">
<return type="Vector2" />
<argument index="0" name="font" type="RID" />
<argument index="1" name="index_a" type="int" />
<argument index="2" name="index_b" type="int" />
<argument index="3" name="size" type="int" />
<method name="font_get_glyph_list" qualifiers="const">
<return type="Array" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="size" type="Vector2i" />
<description>
Returns a kerning of the pair of glyphs.
Returns list of rendered glyphs in the cache entry.
</description>
</method>
<method name="font_get_height" qualifiers="const">
<return type="float" />
<argument index="0" name="font" type="RID" />
<argument index="1" name="size" type="int" />
<method name="font_get_glyph_offset" qualifiers="const">
<return type="Vector2" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="size" type="Vector2i" />
<argument index="2" name="glyph" type="int" />
<description>
Returns the total font height (ascent plus descent) in pixels.
Returns glyph offset from the baseline.
</description>
</method>
<method name="font_get_glyph_size" qualifiers="const">
<return type="Vector2" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="size" type="Vector2i" />
<argument index="2" name="glyph" type="int" />
<description>
Returns size of the glyph.
</description>
</method>
<method name="font_get_glyph_texture_idx" qualifiers="const">
<return type="int" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="size" type="Vector2i" />
<argument index="2" name="glyph" type="int" />
<description>
Returns index of the cache texture containing the glyph.
</description>
</method>
<method name="font_get_glyph_uv_rect" qualifiers="const">
<return type="Rect2" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="size" type="Vector2i" />
<argument index="2" name="glyph" type="int" />
<description>
Returns rectangle in the cache texture containing the glyph.
</description>
</method>
<method name="font_get_hinting" qualifiers="const">
<return type="int" enum="TextServer.Hinting" />
<argument index="0" name="font" type="RID" />
<argument index="0" name="font_rid" type="RID" />
<description>
Returns the font hinting.
Returns the font hinting mode. Used by dynamic fonts only.
</description>
</method>
<method name="font_get_kerning" qualifiers="const">
<return type="Vector2" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="size" type="int" />
<argument index="2" name="glyph_pair" type="Vector2i" />
<description>
Returns kerning for the pair of glyphs.
</description>
</method>
<method name="font_get_kerning_list" qualifiers="const">
<return type="Array" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="size" type="int" />
<description>
Returns list of the kerning overrides.
</description>
</method>
<method name="font_get_language_support_override">
<return type="bool" />
<argument index="0" name="font" type="RID" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="language" type="String" />
<description>
Returns [code]true[/code] if support override is enabled for the [code]language[/code].
@ -241,20 +232,43 @@
</method>
<method name="font_get_language_support_overrides">
<return type="PackedStringArray" />
<argument index="0" name="font" type="RID" />
<argument index="0" name="font_rid" type="RID" />
<description>
Returns list of language support overrides.
</description>
</method>
<method name="font_get_msdf_pixel_range" qualifiers="const">
<return type="int" />
<argument index="0" name="font_rid" type="RID" />
<description>
Return the width of the range around the shape between the minimum and maximum representable signed distance.
</description>
</method>
<method name="font_get_msdf_size" qualifiers="const">
<return type="int" />
<argument index="0" name="font_rid" type="RID" />
<description>
Returns source font size used to generate MSDF textures.
</description>
</method>
<method name="font_get_oversampling" qualifiers="const">
<return type="float" />
<argument index="0" name="font_rid" type="RID" />
<description>
Returns the font oversampling factor, shared by all fonts in the TextServer.
Returns font oversampling factor, if set to [code]0.0[/code] global oversampling factor is used instead. Used by dynamic fonts only.
</description>
</method>
<method name="font_get_scale" qualifiers="const">
<return type="float" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="size" type="int" />
<description>
Returns scaling factor of the color bitmap font.
</description>
</method>
<method name="font_get_script_support_override">
<return type="bool" />
<argument index="0" name="font" type="RID" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="script" type="String" />
<description>
Returns [code]true[/code] if support override is enabled for the [code]script[/code].
@ -262,98 +276,149 @@
</method>
<method name="font_get_script_support_overrides">
<return type="PackedStringArray" />
<argument index="0" name="font" type="RID" />
<argument index="0" name="font_rid" type="RID" />
<description>
Returns list of script support overrides.
</description>
</method>
<method name="font_get_spacing_glyph" qualifiers="const">
<return type="int" />
<argument index="0" name="font" type="RID" />
<method name="font_get_size_cache_list" qualifiers="const">
<return type="Array" />
<argument index="0" name="font_rid" type="RID" />
<description>
Returns extra spacing for each glyph in pixels.
Return list of the font sizes in the cache. Each size is [code]Vector2i[/code] with font size and outline size.
</description>
</method>
<method name="font_get_spacing_space" qualifiers="const">
<method name="font_get_spacing" qualifiers="const">
<return type="int" />
<argument index="0" name="font" type="RID" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="size" type="int" />
<argument index="2" name="spacing" type="int" enum="TextServer.SpacingType" />
<description>
Sets extra spacing for each glyph in pixels.
Returns extra spacing added between glyphs in pixels.
</description>
</method>
<method name="font_get_supported_chars" qualifiers="const">
<return type="String" />
<argument index="0" name="font" type="RID" />
<argument index="0" name="font_rid" type="RID" />
<description>
Returns a string containing all the characters available in the font.
</description>
</method>
<method name="font_get_texture_count" qualifiers="const">
<return type="int" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="size" type="Vector2i" />
<description>
Returns number of textures used by font cache entry.
</description>
</method>
<method name="font_get_texture_image" qualifiers="const">
<return type="Image" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="size" type="Vector2i" />
<argument index="2" name="texture_index" type="int" />
<description>
Returns font cache texture image data.
</description>
</method>
<method name="font_get_texture_offsets" qualifiers="const">
<return type="PackedInt32Array" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="size" type="Vector2i" />
<argument index="2" name="texture_index" type="int" />
<description>
Returns array containing the first free pixel in the each column of texture. Should be the same size as texture width or empty.
</description>
</method>
<method name="font_get_underline_position" qualifiers="const">
<return type="float" />
<argument index="0" name="font" type="RID" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="size" type="int" />
<description>
Returns underline offset (number of pixels below the baseline).
Returns pixel offset of the underline below the baseline.
</description>
</method>
<method name="font_get_underline_thickness" qualifiers="const">
<return type="float" />
<argument index="0" name="font" type="RID" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="size" type="int" />
<description>
Returns underline thickness in pixels.
Returns thickness of the underline in pixels.
</description>
</method>
<method name="font_get_variation" qualifiers="const">
<return type="float" />
<argument index="0" name="font" type="RID" />
<argument index="1" name="tag" type="String" />
<description>
Returns variation coordinate [code]tag[/code].
</description>
</method>
<method name="font_get_variation_list" qualifiers="const">
<method name="font_get_variation_coordinates" qualifiers="const">
<return type="Dictionary" />
<argument index="0" name="font" type="RID" />
<argument index="0" name="font_rid" type="RID" />
<description>
Returns list of supported [url=https://docs.microsoft.com/en-us/typography/opentype/spec/dvaraxisreg]variation coordinates[/url], each coordinate is returned as [code]tag: Vector3i(min_value,max_value,default_value)[/code].
Font variations allow for continuous change of glyph characteristics along some given design axis, such as weight, width or slant.
Returns variation coordinates for the specified font cache entry. See [method font_supported_variation_list] for more info.
</description>
</method>
<method name="font_has_char" qualifiers="const">
<return type="bool" />
<argument index="0" name="font" type="RID" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="char" type="int" />
<description>
Returns [code]true[/code] if [code]char[/code] is available in the font.
Return [code]true[/code] if a Unicode [code]char[/code] is available in the font.
</description>
</method>
<method name="font_has_outline" qualifiers="const">
<method name="font_is_antialiased" qualifiers="const">
<return type="bool" />
<argument index="0" name="font" type="RID" />
<argument index="0" name="font_rid" type="RID" />
<description>
Returns [code]true[/code], if font supports glyph outlines.
Returns [code]true[/code] if font 8-bit anitialiased glyph rendering is supported and enabled.
</description>
</method>
<method name="font_is_force_autohinter" qualifiers="const">
<return type="bool" />
<argument index="0" name="font_rid" type="RID" />
<description>
Returns [code]true[/code] if auto-hinting is supported and preffered over font built-in hinting. Used by dynamic fonts only.
</description>
</method>
<method name="font_is_language_supported" qualifiers="const">
<return type="bool" />
<argument index="0" name="font" type="RID" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="language" type="String" />
<description>
Returns [code]true[/code], if font supports given language (ISO 639 code).
Returns [code]true[/code], if font supports given language ([url=https://en.wikipedia.org/wiki/ISO_639-1]ISO 639[/url] code).
</description>
</method>
<method name="font_is_multichannel_signed_distance_field" qualifiers="const">
<return type="bool" />
<argument index="0" name="font_rid" type="RID" />
<description>
Returns [code]true[/code] if glyphs of all sizes are rendered using single multichannel signed distance field generated from the dynamic font vector data.
</description>
</method>
<method name="font_is_script_supported" qualifiers="const">
<return type="bool" />
<argument index="0" name="font" type="RID" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="script" type="String" />
<description>
Returns [code]true[/code], if font supports given script (ISO 15924 code).
</description>
</method>
<method name="font_remove_glyph">
<return type="void" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="size" type="Vector2i" />
<argument index="2" name="glyph" type="int" />
<description>
Removes specified rendered glyph information from the cache entry. Note: This function will not remove textures associated with the glyphs, use [method font_remove_texture] to remove them manually.
</description>
</method>
<method name="font_remove_kerning">
<return type="void" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="size" type="int" />
<argument index="2" name="glyph_pair" type="Vector2i" />
<description>
Removes kerning override for the pair of glyphs.
</description>
</method>
<method name="font_remove_language_support_override">
<return type="void" />
<argument index="0" name="font" type="RID" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="language" type="String" />
<description>
Remove language support override.
@ -361,92 +426,299 @@
</method>
<method name="font_remove_script_support_override">
<return type="void" />
<argument index="0" name="font" type="RID" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="script" type="String" />
<description>
Removes script support override.
</description>
</method>
<method name="font_set_antialiased">
<method name="font_remove_size_cache">
<return type="void" />
<argument index="0" name="font" type="RID" />
<argument index="1" name="antialiased" type="bool" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="size" type="Vector2i" />
<description>
Sets font anti-aliasing.
Removes specified font size from the cache entry.
</description>
</method>
<method name="font_set_distance_field_hint">
<method name="font_remove_texture">
<return type="void" />
<argument index="0" name="font" type="RID" />
<argument index="1" name="distance_field" type="bool" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="size" type="Vector2i" />
<argument index="2" name="texture_index" type="int" />
<description>
Sets font distance field hint.
Removes specified texture from font cache entry. Note: This function will not remove glyphs associated with the texture, remove them manually, using [method font_remove_glyph].
</description>
</method>
<method name="font_render_glyph">
<return type="void" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="size" type="Vector2i" />
<argument index="2" name="index" type="int" />
<description>
Renders specified glyph the the font cache texture.
</description>
</method>
<method name="font_render_range">
<return type="void" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="size" type="Vector2i" />
<argument index="2" name="start" type="int" />
<argument index="3" name="end" type="int" />
<description>
Renders the range of characters to the font cache texture.
</description>
</method>
<method name="font_set_antialiased">
<return type="void" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="antialiased" type="bool" />
<description>
If set to [code]true[/code], 8-bit antialiased glyph rendering is used, otherwise 1-bit rendering is used. Used by dynamic fonts only.
</description>
</method>
<method name="font_set_ascent">
<return type="void" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="size" type="int" />
<argument index="2" name="ascent" type="float" />
<description>
Sets the font ascent (number of pixels above the baseline).
</description>
</method>
<method name="font_set_data">
<return type="void" />
<argument index="0" name="data" type="RID" />
<argument index="1" name="arg1" type="PackedByteArray" />
<description>
Sets font source data, e.g contents of the dynamic font source file.
</description>
</method>
<method name="font_set_descent">
<return type="void" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="size" type="int" />
<argument index="2" name="descent" type="float" />
<description>
Sets the font descent (number of pixels below the baseline).
</description>
</method>
<method name="font_set_fixed_size">
<return type="void" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="fixed_size" type="int" />
<description>
Sets bitmap font fixed size. If set to value greater than zero, same cache entry will be used for all font sizes.
</description>
</method>
<method name="font_set_force_autohinter">
<return type="void" />
<argument index="0" name="font" type="RID" />
<argument index="1" name="enabeld" type="bool" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="force_autohinter" type="bool" />
<description>
Enables/disables default autohinter.
If set to [code]true[/code] auto-hinting is preffered over font built-in hinting.
</description>
</method>
<method name="font_set_global_oversampling">
<return type="void" />
<argument index="0" name="oversampling" type="float" />
<description>
Sets oversampling factor, shared by all font in the TextServer.
Note: This value can be automaticaly changed by display server.
</description>
</method>
<method name="font_set_glyph_advance">
<return type="void" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="size" type="int" />
<argument index="2" name="glyph" type="int" />
<argument index="3" name="advance" type="Vector2" />
<description>
Sets glyph advance (offset of the next glyph). Note: advance for glyphs outlines is the same as the base glyph advance and is not saved.
</description>
</method>
<method name="font_set_glyph_offset">
<return type="void" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="size" type="Vector2i" />
<argument index="2" name="glyph" type="int" />
<argument index="3" name="offset" type="Vector2" />
<description>
Sets glyph offset from the baseline.
</description>
</method>
<method name="font_set_glyph_size">
<return type="void" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="size" type="Vector2i" />
<argument index="2" name="glyph" type="int" />
<argument index="3" name="size" type="Vector2" />
<description>
Sets size of the glyph.
</description>
</method>
<method name="font_set_glyph_texture_idx">
<return type="void" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="size" type="Vector2i" />
<argument index="2" name="glyph" type="int" />
<argument index="3" name="texture_idx" type="int" />
<description>
Sets index of the cache texture containing the glyph.
</description>
</method>
<method name="font_set_glyph_uv_rect">
<return type="void" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="size" type="Vector2i" />
<argument index="2" name="glyph" type="int" />
<argument index="3" name="uv_rect" type="Rect2" />
<description>
Sets rectangle in the cache texture containing the glyph.
</description>
</method>
<method name="font_set_hinting">
<return type="void" />
<argument index="0" name="font" type="RID" />
<argument index="1" name="hinting" type="int" enum="TextServer.Hinting" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="_hinting" type="int" enum="TextServer.Hinting" />
<description>
Sets font hinting.
Sets font hinting mode. Used by dynamic fonts only.
</description>
</method>
<method name="font_set_kerning">
<return type="void" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="size" type="int" />
<argument index="2" name="glyph_pair" type="Vector2i" />
<argument index="3" name="kerning" type="Vector2" />
<description>
Sets kerning for the pair of glyphs.
</description>
</method>
<method name="font_set_language_support_override">
<return type="void" />
<argument index="0" name="font" type="RID" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="language" type="String" />
<argument index="2" name="supported" type="bool" />
<description>
Adds override for [method font_is_language_supported].
</description>
</method>
<method name="font_set_msdf_pixel_range">
<return type="void" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="msdf_pixel_range" type="int" />
<description>
Sets the width of the range around the shape between the minimum and maximum representable signed distance.
</description>
</method>
<method name="font_set_msdf_size">
<return type="void" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="msdf_size" type="int" />
<description>
Sets source font size used to generate MSDF textures.
</description>
</method>
<method name="font_set_multichannel_signed_distance_field">
<return type="void" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="msdf" type="bool" />
<description>
If set to [code]true[/code], glyphs of all sizes are rendered using single multichannel signed distance field generated from the dynamic font vector data.
</description>
</method>
<method name="font_set_oversampling">
<return type="void" />
<argument index="0" name="oversampling" type="float" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="oversampling" type="float" />
<description>
Sets oversampling factor, shared by all font in the TextServer.
Sets font oversampling factor, if set to [code]0.0[/code] global oversampling factor is used instead. Used by dynamic fonts only.
</description>
</method>
<method name="font_set_scale">
<return type="void" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="size" type="int" />
<argument index="2" name="scale" type="float" />
<description>
Sets scaling factor of the color bitmap font.
</description>
</method>
<method name="font_set_script_support_override">
<return type="void" />
<argument index="0" name="font" type="RID" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="script" type="String" />
<argument index="2" name="supported" type="bool" />
<description>
Adds override for [method font_is_script_supported].
</description>
</method>
<method name="font_set_spacing_glyph">
<method name="font_set_spacing">
<return type="void" />
<argument index="0" name="font" type="RID" />
<argument index="1" name="value" type="int" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="size" type="int" />
<argument index="2" name="spacing" type="int" enum="TextServer.SpacingType" />
<argument index="3" name="value" type="int" />
<description>
Returns extra spacing for the space character in pixels.
Sets extra spacing added between glyphs in pixels.
</description>
</method>
<method name="font_set_spacing_space">
<method name="font_set_texture_image">
<return type="void" />
<argument index="0" name="font" type="RID" />
<argument index="1" name="value" type="int" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="size" type="Vector2i" />
<argument index="2" name="texture_index" type="int" />
<argument index="3" name="image" type="Image" />
<description>
Sets extra spacing for the space character in pixels.
Sets font cache texture image data.
</description>
</method>
<method name="font_set_variation">
<method name="font_set_texture_offsets">
<return type="void" />
<argument index="0" name="font" type="RID" />
<argument index="1" name="tag" type="String" />
<argument index="2" name="value" type="float" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="size" type="Vector2i" />
<argument index="2" name="texture_index" type="int" />
<argument index="3" name="offset" type="PackedInt32Array" />
<description>
Sets array containing the first free pixel in the each column of texture. Should be the same size as texture width or empty.
</description>
</method>
<method name="font_set_underline_position">
<return type="void" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="size" type="int" />
<argument index="2" name="underline_position" type="float" />
<description>
Sets pixel offset of the underline below the baseline.
</description>
</method>
<method name="font_set_underline_thickness">
<return type="void" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="size" type="int" />
<argument index="2" name="underline_thickness" type="float" />
<description>
Sets thickness of the underline in pixels.
</description>
</method>
<method name="font_set_variation_coordinates">
<return type="void" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="variation_coordinates" type="Dictionary" />
<description>
Sets variation coordinates for the specified font cache entry. See [method font_supported_variation_list] for more info.
</description>
</method>
<method name="font_supported_feature_list" qualifiers="const">
<return type="Dictionary" />
<argument index="0" name="font_rid" type="RID" />
<description>
</description>
</method>
<method name="font_supported_variation_list" qualifiers="const">
<return type="Dictionary" />
<argument index="0" name="font_rid" type="RID" />
<description>
Sets variation coordinate [code]name[/code]. Unsupported coordinates will be silently ignored.
</description>
</method>
<method name="format_number" qualifiers="const">
@ -478,13 +750,6 @@
Returns the name of the server interface.
</description>
</method>
<method name="get_system_fonts" qualifiers="const">
<return type="PackedStringArray" />
<description>
Returns list of available system fonts.
Note: This method is supported by servers with the [code]FEATURE_FONT_SYSTEM[/code] feature.
</description>
</method>
<method name="has">
<return type="bool" />
<argument index="0" name="rid" type="RID" />
@ -514,7 +779,7 @@
Note: This function should be called before any other TextServer functions used, otherwise it won't have any effect.
</description>
</method>
<method name="name_to_tag">
<method name="name_to_tag" qualifiers="const">
<return type="int" />
<argument index="0" name="name" type="String" />
<description>
@ -890,7 +1155,7 @@
Aligns shaped text to the given tab-stops.
</description>
</method>
<method name="tag_to_name">
<method name="tag_to_name" qualifiers="const">
<return type="String" />
<argument index="0" name="tag" type="int" />
<description>
@ -1023,5 +1288,17 @@
<constant name="CONTOUR_CURVE_TAG_OFF_CUBIC" value="2" enum="ContourPointTag">
Contour point isn't on the curve, but serves as a control point for a cubic Bézier arc.
</constant>
<constant name="SPACING_GLYPH" value="0" enum="SpacingType">
Spacing for each glyph.
</constant>
<constant name="SPACING_SPACE" value="1" enum="SpacingType">
Spacing for the space character.
</constant>
<constant name="SPACING_TOP" value="2" enum="SpacingType">
Spacing at the top of the line.
</constant>
<constant name="SPACING_BOTTOM" value="3" enum="SpacingType">
Spacing at the bottom of the line.
</constant>
</constants>
</class>

View File

@ -140,6 +140,14 @@
<return type="Font" />
<argument index="0" name="column" type="int" />
<description>
Returns custom font used to draw text in the column [code]column[/code].
</description>
</method>
<method name="get_custom_font_size" qualifiers="const">
<return type="int" />
<argument index="0" name="column" type="int" />
<description>
Returns custom font size used to draw text in the column [code]column[/code].
</description>
</method>
<method name="get_expand_right" qualifiers="const">
@ -464,6 +472,15 @@
<argument index="0" name="column" type="int" />
<argument index="1" name="font" type="Font" />
<description>
Sets custom font used to draw text in the column [code]column[/code].
</description>
</method>
<method name="set_custom_font_size">
<return type="void" />
<argument index="0" name="column" type="int" />
<argument index="1" name="font_size" type="int" />
<description>
Sets custom font size used to draw text in the column [code]column[/code].
</description>
</method>
<method name="set_editable">

View File

@ -67,46 +67,113 @@
m_name->add_data(FontJapanese); \
m_name->add_data(FontFallback);
// the custom spacings might only work with Noto Sans
#define MAKE_DEFAULT_FONT(m_name) \
Ref<Font> m_name; \
m_name.instantiate(); \
if (CustomFont.is_valid()) { \
m_name->add_data(CustomFont); \
m_name->add_data(DefaultFont); \
} else { \
m_name->add_data(DefaultFont); \
} \
m_name->set_spacing(Font::SPACING_TOP, -EDSCALE); \
m_name->set_spacing(Font::SPACING_BOTTOM, -EDSCALE); \
#define MAKE_DEFAULT_FONT(m_name, m_variations, m_base_size) \
Ref<Font> m_name; \
m_name.instantiate(); \
if (CustomFont.is_valid()) { \
m_name->add_data(CustomFont); \
m_name->add_data(DefaultFont); \
} else { \
m_name->add_data(DefaultFont); \
} \
{ \
Dictionary variations; \
if (m_variations != String()) { \
Vector<String> variation_tags = m_variations.split(","); \
for (int i = 0; i < variation_tags.size(); i++) { \
Vector<String> tokens = variation_tags[i].split("="); \
if (tokens.size() == 2) { \
variations[tokens[0]] = tokens[1].to_float(); \
} \
} \
} \
m_name->set_variation_coordinates(variations); \
} \
m_name->set_base_size(m_base_size); \
m_name->set_spacing(TextServer::SPACING_TOP, -EDSCALE); \
m_name->set_spacing(TextServer::SPACING_BOTTOM, -EDSCALE); \
MAKE_FALLBACKS(m_name);
#define MAKE_BOLD_FONT(m_name) \
Ref<Font> m_name; \
m_name.instantiate(); \
if (CustomFontBold.is_valid()) { \
m_name->add_data(CustomFontBold); \
m_name->add_data(DefaultFontBold); \
} else { \
m_name->add_data(DefaultFontBold); \
} \
m_name->set_spacing(Font::SPACING_TOP, -EDSCALE); \
m_name->set_spacing(Font::SPACING_BOTTOM, -EDSCALE); \
#define MAKE_BOLD_FONT(m_name, m_variations, m_base_size) \
Ref<Font> m_name; \
m_name.instantiate(); \
if (CustomFontBold.is_valid()) { \
m_name->add_data(CustomFontBold); \
m_name->add_data(DefaultFontBold); \
} else { \
m_name->add_data(DefaultFontBold); \
} \
{ \
Dictionary variations; \
if (m_variations != String()) { \
Vector<String> variation_tags = m_variations.split(","); \
for (int i = 0; i < variation_tags.size(); i++) { \
Vector<String> tokens = variation_tags[i].split("="); \
if (tokens.size() == 2) { \
variations[tokens[0]] = tokens[1].to_float(); \
} \
} \
} \
m_name->set_variation_coordinates(variations); \
} \
m_name->set_base_size(m_base_size); \
m_name->set_spacing(TextServer::SPACING_TOP, -EDSCALE); \
m_name->set_spacing(TextServer::SPACING_BOTTOM, -EDSCALE); \
MAKE_FALLBACKS_BOLD(m_name);
#define MAKE_SOURCE_FONT(m_name) \
Ref<Font> m_name; \
m_name.instantiate(); \
if (CustomFontSource.is_valid()) { \
m_name->add_data(CustomFontSource); \
m_name->add_data(dfmono); \
} else { \
m_name->add_data(dfmono); \
} \
m_name->set_spacing(Font::SPACING_TOP, -EDSCALE); \
m_name->set_spacing(Font::SPACING_BOTTOM, -EDSCALE); \
#define MAKE_SOURCE_FONT(m_name, m_variations, m_base_size) \
Ref<Font> m_name; \
m_name.instantiate(); \
if (CustomFontSource.is_valid()) { \
m_name->add_data(CustomFontSource); \
m_name->add_data(dfmono); \
} else { \
m_name->add_data(dfmono); \
} \
{ \
Dictionary variations; \
if (m_variations != String()) { \
Vector<String> variation_tags = m_variations.split(","); \
for (int i = 0; i < variation_tags.size(); i++) { \
Vector<String> tokens = variation_tags[i].split("="); \
if (tokens.size() == 2) { \
variations[tokens[0]] = tokens[1].to_float(); \
} \
} \
} \
m_name->set_variation_coordinates(variations); \
} \
m_name->set_base_size(m_base_size); \
m_name->set_spacing(TextServer::SPACING_TOP, -EDSCALE); \
m_name->set_spacing(TextServer::SPACING_BOTTOM, -EDSCALE); \
MAKE_FALLBACKS(m_name);
Ref<FontData> load_cached_external_font(const String &p_path, TextServer::Hinting p_hinting, bool p_aa, bool p_autohint) {
Ref<FontData> font;
font.instantiate();
Vector<uint8_t> data = FileAccess::get_file_as_array(p_path);
font->set_data(data);
font->set_antialiased(p_aa);
font->set_hinting(p_hinting);
font->set_force_autohinter(p_autohint);
return font;
}
Ref<FontData> load_cached_internal_font(const uint8_t *p_data, size_t p_size, TextServer::Hinting p_hinting, bool p_aa, bool p_autohint) {
Ref<FontData> font;
font.instantiate();
font->set_data_ptr(p_data, p_size);
font->set_antialiased(p_aa);
font->set_hinting(p_hinting);
font->set_force_autohinter(p_autohint);
return font;
}
void editor_register_fonts(Ref<Theme> p_theme) {
DirAccess *dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
@ -144,11 +211,7 @@ void editor_register_fonts(Ref<Theme> p_theme) {
String custom_font_path = EditorSettings::get_singleton()->get("interface/editor/main_font");
Ref<FontData> CustomFont;
if (custom_font_path.length() > 0 && dir->file_exists(custom_font_path)) {
CustomFont.instantiate();
CustomFont->load_resource(custom_font_path, default_font_size);
CustomFont->set_antialiased(font_antialiased);
CustomFont->set_hinting(font_hinting);
CustomFont->set_force_autohinter(true); //just looks better..i think?
CustomFont = load_cached_external_font(custom_font_path, font_hinting, font_antialiased, true);
} else {
EditorSettings::get_singleton()->set_manually("interface/editor/main_font", "");
}
@ -158,11 +221,7 @@ void editor_register_fonts(Ref<Theme> p_theme) {
String custom_font_path_bold = EditorSettings::get_singleton()->get("interface/editor/main_font_bold");
Ref<FontData> CustomFontBold;
if (custom_font_path_bold.length() > 0 && dir->file_exists(custom_font_path_bold)) {
CustomFontBold.instantiate();
CustomFontBold->load_resource(custom_font_path_bold, default_font_size);
CustomFontBold->set_antialiased(font_antialiased);
CustomFontBold->set_hinting(font_hinting);
CustomFontBold->set_force_autohinter(true); //just looks better..i think?
CustomFontBold = load_cached_external_font(custom_font_path_bold, font_hinting, font_antialiased, true);
} else {
EditorSettings::get_singleton()->set_manually("interface/editor/main_font_bold", "");
}
@ -172,231 +231,51 @@ void editor_register_fonts(Ref<Theme> p_theme) {
String custom_font_path_source = EditorSettings::get_singleton()->get("interface/editor/code_font");
Ref<FontData> CustomFontSource;
if (custom_font_path_source.length() > 0 && dir->file_exists(custom_font_path_source)) {
CustomFontSource.instantiate();
CustomFontSource->load_resource(custom_font_path_source, default_font_size);
CustomFontSource->set_antialiased(font_antialiased);
CustomFontSource->set_hinting(font_hinting);
Vector<String> subtag = String(EditorSettings::get_singleton()->get("interface/editor/code_font_custom_variations")).split(",");
for (int i = 0; i < subtag.size(); i++) {
Vector<String> subtag_a = subtag[i].split("=");
if (subtag_a.size() == 2) {
CustomFontSource->set_variation(subtag_a[0], subtag_a[1].to_float());
}
}
CustomFontSource = load_cached_external_font(custom_font_path_source, font_hinting, font_antialiased, true);
} else {
EditorSettings::get_singleton()->set_manually("interface/editor/code_font", "");
}
memdelete(dir);
/* Noto Sans UI */
/* Noto Sans */
Ref<FontData> DefaultFont;
DefaultFont.instantiate();
DefaultFont->load_memory(_font_NotoSans_Regular, _font_NotoSans_Regular_size, "ttf", default_font_size);
DefaultFont->set_antialiased(font_antialiased);
DefaultFont->set_hinting(font_hinting);
DefaultFont->set_force_autohinter(true); //just looks better..i think?
Ref<FontData> DefaultFont = load_cached_internal_font(_font_NotoSans_Regular, _font_NotoSans_Regular_size, font_hinting, font_antialiased, true);
Ref<FontData> DefaultFontBold = load_cached_internal_font(_font_NotoSans_Bold, _font_NotoSans_Bold_size, font_hinting, font_antialiased, true);
Ref<FontData> FontArabic = load_cached_internal_font(_font_NotoNaskhArabicUI_Regular, _font_NotoNaskhArabicUI_Regular_size, font_hinting, font_antialiased, true);
Ref<FontData> FontArabicBold = load_cached_internal_font(_font_NotoNaskhArabicUI_Bold, _font_NotoNaskhArabicUI_Bold_size, font_hinting, font_antialiased, true);
Ref<FontData> FontBengali = load_cached_internal_font(_font_NotoSansBengaliUI_Regular, _font_NotoSansBengaliUI_Regular_size, font_hinting, font_antialiased, true);
Ref<FontData> FontBengaliBold = load_cached_internal_font(_font_NotoSansBengaliUI_Bold, _font_NotoSansBengaliUI_Bold_size, font_hinting, font_antialiased, true);
Ref<FontData> FontDevanagari = load_cached_internal_font(_font_NotoSansDevanagariUI_Regular, _font_NotoSansDevanagariUI_Regular_size, font_hinting, font_antialiased, true);
Ref<FontData> FontDevanagariBold = load_cached_internal_font(_font_NotoSansDevanagariUI_Bold, _font_NotoSansDevanagariUI_Bold_size, font_hinting, font_antialiased, true);
Ref<FontData> FontGeorgian = load_cached_internal_font(_font_NotoSansGeorgian_Regular, _font_NotoSansGeorgian_Regular_size, font_hinting, font_antialiased, true);
Ref<FontData> FontGeorgianBold = load_cached_internal_font(_font_NotoSansGeorgian_Bold, _font_NotoSansGeorgian_Bold_size, font_hinting, font_antialiased, true);
Ref<FontData> FontHebrew = load_cached_internal_font(_font_NotoSansHebrew_Regular, _font_NotoSansHebrew_Regular_size, font_hinting, font_antialiased, true);
Ref<FontData> FontHebrewBold = load_cached_internal_font(_font_NotoSansHebrew_Bold, _font_NotoSansHebrew_Bold_size, font_hinting, font_antialiased, true);
Ref<FontData> FontMalayalam = load_cached_internal_font(_font_NotoSansMalayalamUI_Regular, _font_NotoSansMalayalamUI_Regular_size, font_hinting, font_antialiased, true);
Ref<FontData> FontMalayalamBold = load_cached_internal_font(_font_NotoSansMalayalamUI_Bold, _font_NotoSansMalayalamUI_Bold_size, font_hinting, font_antialiased, true);
Ref<FontData> FontOriya = load_cached_internal_font(_font_NotoSansOriyaUI_Regular, _font_NotoSansOriyaUI_Regular_size, font_hinting, font_antialiased, true);
Ref<FontData> FontOriyaBold = load_cached_internal_font(_font_NotoSansOriyaUI_Bold, _font_NotoSansOriyaUI_Bold_size, font_hinting, font_antialiased, true);
Ref<FontData> FontSinhala = load_cached_internal_font(_font_NotoSansSinhalaUI_Regular, _font_NotoSansSinhalaUI_Regular_size, font_hinting, font_antialiased, true);
Ref<FontData> FontSinhalaBold = load_cached_internal_font(_font_NotoSansSinhalaUI_Bold, _font_NotoSansSinhalaUI_Bold_size, font_hinting, font_antialiased, true);
Ref<FontData> FontTamil = load_cached_internal_font(_font_NotoSansTamilUI_Regular, _font_NotoSansTamilUI_Regular_size, font_hinting, font_antialiased, true);
Ref<FontData> FontTamilBold = load_cached_internal_font(_font_NotoSansTamilUI_Bold, _font_NotoSansTamilUI_Bold_size, font_hinting, font_antialiased, true);
Ref<FontData> FontTelugu = load_cached_internal_font(_font_NotoSansTeluguUI_Regular, _font_NotoSansTeluguUI_Regular_size, font_hinting, font_antialiased, true);
Ref<FontData> FontTeluguBold = load_cached_internal_font(_font_NotoSansTeluguUI_Bold, _font_NotoSansTeluguUI_Bold_size, font_hinting, font_antialiased, true);
Ref<FontData> FontThai = load_cached_internal_font(_font_NotoSansThaiUI_Regular, _font_NotoSansThaiUI_Regular_size, font_hinting, font_antialiased, true);
Ref<FontData> FontThaiBold = load_cached_internal_font(_font_NotoSansThaiUI_Bold, _font_NotoSansThaiUI_Bold_size, font_hinting, font_antialiased, true);
Ref<FontData> DefaultFontBold;
DefaultFontBold.instantiate();
DefaultFontBold->load_memory(_font_NotoSans_Bold, _font_NotoSans_Bold_size, "ttf", default_font_size);
DefaultFontBold->set_antialiased(font_antialiased);
DefaultFontBold->set_hinting(font_hinting);
DefaultFontBold->set_force_autohinter(true); // just looks better..i think?
/* Droid Sans */
Ref<FontData> FontArabic;
FontArabic.instantiate();
FontArabic->load_memory(_font_NotoNaskhArabicUI_Regular, _font_NotoNaskhArabicUI_Regular_size, "ttf", default_font_size);
FontArabic->set_antialiased(font_antialiased);
FontArabic->set_hinting(font_hinting);
FontArabic->set_force_autohinter(true); //just looks better..i think?
Ref<FontData> FontArabicBold;
FontArabicBold.instantiate();
FontArabicBold->load_memory(_font_NotoNaskhArabicUI_Bold, _font_NotoNaskhArabicUI_Bold_size, "ttf", default_font_size);
FontArabicBold->set_antialiased(font_antialiased);
FontArabicBold->set_hinting(font_hinting);
FontArabicBold->set_force_autohinter(true); //just looks better..i think?
Ref<FontData> FontBengali;
FontBengali.instantiate();
FontBengali->load_memory(_font_NotoSansBengaliUI_Regular, _font_NotoSansBengaliUI_Regular_size, "ttf", default_font_size);
FontBengali->set_antialiased(font_antialiased);
FontBengali->set_hinting(font_hinting);
FontBengali->set_force_autohinter(true); //just looks better..i think?
Ref<FontData> FontBengaliBold;
FontBengaliBold.instantiate();
FontBengaliBold->load_memory(_font_NotoSansBengaliUI_Bold, _font_NotoSansBengaliUI_Bold_size, "ttf", default_font_size);
FontBengaliBold->set_antialiased(font_antialiased);
FontBengaliBold->set_hinting(font_hinting);
FontBengaliBold->set_force_autohinter(true); //just looks better..i think?
Ref<FontData> FontDevanagari;
FontDevanagari.instantiate();
FontDevanagari->load_memory(_font_NotoSansDevanagariUI_Regular, _font_NotoSansDevanagariUI_Regular_size, "ttf", default_font_size);
FontDevanagari->set_antialiased(font_antialiased);
FontDevanagari->set_hinting(font_hinting);
FontDevanagari->set_force_autohinter(true); //just looks better..i think?
Ref<FontData> FontDevanagariBold;
FontDevanagariBold.instantiate();
FontDevanagariBold->load_memory(_font_NotoSansDevanagariUI_Bold, _font_NotoSansDevanagariUI_Bold_size, "ttf", default_font_size);
FontDevanagariBold->set_antialiased(font_antialiased);
FontDevanagariBold->set_hinting(font_hinting);
FontDevanagariBold->set_force_autohinter(true); //just looks better..i think?
Ref<FontData> FontGeorgian;
FontGeorgian.instantiate();
FontGeorgian->load_memory(_font_NotoSansGeorgian_Regular, _font_NotoSansGeorgian_Regular_size, "ttf", default_font_size);
FontGeorgian->set_antialiased(font_antialiased);
FontGeorgian->set_hinting(font_hinting);
FontGeorgian->set_force_autohinter(true); //just looks better..i think?
Ref<FontData> FontGeorgianBold;
FontGeorgianBold.instantiate();
FontGeorgianBold->load_memory(_font_NotoSansGeorgian_Bold, _font_NotoSansGeorgian_Bold_size, "ttf", default_font_size);
FontGeorgianBold->set_antialiased(font_antialiased);
FontGeorgianBold->set_hinting(font_hinting);
FontGeorgianBold->set_force_autohinter(true); //just looks better..i think?
Ref<FontData> FontHebrew;
FontHebrew.instantiate();
FontHebrew->load_memory(_font_NotoSansHebrew_Regular, _font_NotoSansHebrew_Regular_size, "ttf", default_font_size);
FontHebrew->set_antialiased(font_antialiased);
FontHebrew->set_hinting(font_hinting);
FontHebrew->set_force_autohinter(true); //just looks better..i think?
Ref<FontData> FontHebrewBold;
FontHebrewBold.instantiate();
FontHebrewBold->load_memory(_font_NotoSansHebrew_Bold, _font_NotoSansHebrew_Bold_size, "ttf", default_font_size);
FontHebrewBold->set_antialiased(font_antialiased);
FontHebrewBold->set_hinting(font_hinting);
FontHebrewBold->set_force_autohinter(true); //just looks better..i think?
Ref<FontData> FontMalayalam;
FontMalayalam.instantiate();
FontMalayalam->load_memory(_font_NotoSansMalayalamUI_Regular, _font_NotoSansMalayalamUI_Regular_size, "ttf", default_font_size);
FontMalayalam->set_antialiased(font_antialiased);
FontMalayalam->set_hinting(font_hinting);
FontMalayalam->set_force_autohinter(true); //just looks better..i think?
Ref<FontData> FontMalayalamBold;
FontMalayalamBold.instantiate();
FontMalayalamBold->load_memory(_font_NotoSansMalayalamUI_Bold, _font_NotoSansMalayalamUI_Bold_size, "ttf", default_font_size);
FontMalayalamBold->set_antialiased(font_antialiased);
FontMalayalamBold->set_hinting(font_hinting);
FontMalayalamBold->set_force_autohinter(true); //just looks better..i think?
Ref<FontData> FontOriya;
FontOriya.instantiate();
FontOriya->load_memory(_font_NotoSansOriyaUI_Regular, _font_NotoSansOriyaUI_Regular_size, "ttf", default_font_size);
FontOriya->set_antialiased(font_antialiased);
FontOriya->set_hinting(font_hinting);
FontOriya->set_force_autohinter(true); //just looks better..i think?
Ref<FontData> FontOriyaBold;
FontOriyaBold.instantiate();
FontOriyaBold->load_memory(_font_NotoSansOriyaUI_Bold, _font_NotoSansOriyaUI_Bold_size, "ttf", default_font_size);
FontOriyaBold->set_antialiased(font_antialiased);
FontOriyaBold->set_hinting(font_hinting);
FontOriyaBold->set_force_autohinter(true); //just looks better..i think?
Ref<FontData> FontSinhala;
FontSinhala.instantiate();
FontSinhala->load_memory(_font_NotoSansSinhalaUI_Regular, _font_NotoSansSinhalaUI_Regular_size, "ttf", default_font_size);
FontSinhala->set_antialiased(font_antialiased);
FontSinhala->set_hinting(font_hinting);
FontSinhala->set_force_autohinter(true); //just looks better..i think?
Ref<FontData> FontSinhalaBold;
FontSinhalaBold.instantiate();
FontSinhalaBold->load_memory(_font_NotoSansSinhalaUI_Bold, _font_NotoSansSinhalaUI_Bold_size, "ttf", default_font_size);
FontSinhalaBold->set_antialiased(font_antialiased);
FontSinhalaBold->set_hinting(font_hinting);
FontSinhalaBold->set_force_autohinter(true); //just looks better..i think?
Ref<FontData> FontTamil;
FontTamil.instantiate();
FontTamil->load_memory(_font_NotoSansTamilUI_Regular, _font_NotoSansTamilUI_Regular_size, "ttf", default_font_size);
FontTamil->set_antialiased(font_antialiased);
FontTamil->set_hinting(font_hinting);
FontTamil->set_force_autohinter(true); //just looks better..i think?
Ref<FontData> FontTamilBold;
FontTamilBold.instantiate();
FontTamilBold->load_memory(_font_NotoSansTamilUI_Bold, _font_NotoSansTamilUI_Bold_size, "ttf", default_font_size);
FontTamilBold->set_antialiased(font_antialiased);
FontTamilBold->set_hinting(font_hinting);
FontTamilBold->set_force_autohinter(true); //just looks better..i think?
Ref<FontData> FontTelugu;
FontTelugu.instantiate();
FontTelugu->load_memory(_font_NotoSansTeluguUI_Regular, _font_NotoSansTeluguUI_Regular_size, "ttf", default_font_size);
FontTelugu->set_antialiased(font_antialiased);
FontTelugu->set_hinting(font_hinting);
FontTelugu->set_force_autohinter(true); //just looks better..i think?
Ref<FontData> FontTeluguBold;
FontTeluguBold.instantiate();
FontTeluguBold->load_memory(_font_NotoSansTeluguUI_Bold, _font_NotoSansTeluguUI_Bold_size, "ttf", default_font_size);
FontTeluguBold->set_antialiased(font_antialiased);
FontTeluguBold->set_hinting(font_hinting);
FontTeluguBold->set_force_autohinter(true); //just looks better..i think?
Ref<FontData> FontThai;
FontThai.instantiate();
FontThai->load_memory(_font_NotoSansThaiUI_Regular, _font_NotoSansThaiUI_Regular_size, "ttf", default_font_size);
FontThai->set_antialiased(font_antialiased);
FontThai->set_hinting(font_hinting);
FontThai->set_force_autohinter(true); //just looks better..i think?
Ref<FontData> FontThaiBold;
FontThaiBold.instantiate();
FontThaiBold->load_memory(_font_NotoSansThaiUI_Bold, _font_NotoSansThaiUI_Bold_size, "ttf", default_font_size);
FontThaiBold->set_antialiased(font_antialiased);
FontThaiBold->set_hinting(font_hinting);
FontThaiBold->set_force_autohinter(true); //just looks better..i think?
/* Droid Sans Fallback */
Ref<FontData> FontFallback;
FontFallback.instantiate();
FontFallback->load_memory(_font_DroidSansFallback, _font_DroidSansFallback_size, "ttf", default_font_size);
FontFallback->set_antialiased(font_antialiased);
FontFallback->set_hinting(font_hinting);
FontFallback->set_force_autohinter(true); //just looks better..i think?
/* Droid Sans Japanese */
Ref<FontData> FontJapanese;
FontJapanese.instantiate();
FontJapanese->load_memory(_font_DroidSansJapanese, _font_DroidSansJapanese_size, "ttf", default_font_size);
FontJapanese->set_antialiased(font_antialiased);
FontJapanese->set_hinting(font_hinting);
FontJapanese->set_force_autohinter(true); //just looks better..i think?
Ref<FontData> FontFallback = load_cached_internal_font(_font_DroidSansFallback, _font_DroidSansFallback_size, font_hinting, font_antialiased, true);
Ref<FontData> FontJapanese = load_cached_internal_font(_font_DroidSansJapanese, _font_DroidSansJapanese_size, font_hinting, font_antialiased, true);
/* Hack */
Ref<FontData> dfmono;
dfmono.instantiate();
dfmono->load_memory(_font_Hack_Regular, _font_Hack_Regular_size, "ttf", default_font_size);
dfmono->set_antialiased(font_antialiased);
dfmono->set_hinting(font_hinting);
Vector<String> subtag = String(EditorSettings::get_singleton()->get("interface/editor/code_font_custom_variations")).split(",");
Dictionary ftrs;
for (int i = 0; i < subtag.size(); i++) {
Vector<String> subtag_a = subtag[i].split("=");
if (subtag_a.size() == 2) {
dfmono->set_variation(subtag_a[0], subtag_a[1].to_float());
}
}
Ref<FontData> dfmono = load_cached_internal_font(_font_Hack_Regular, _font_Hack_Regular_size, font_hinting, font_antialiased, true);
// Default font
MAKE_DEFAULT_FONT(df);
MAKE_DEFAULT_FONT(df, String(), default_font_size);
p_theme->set_default_theme_font(df); // Default theme font
p_theme->set_default_theme_font_size(default_font_size);
@ -404,7 +283,7 @@ void editor_register_fonts(Ref<Theme> p_theme) {
p_theme->set_font("main", "EditorFonts", df);
// Bold font
MAKE_BOLD_FONT(df_bold);
MAKE_BOLD_FONT(df_bold, String(), default_font_size);
p_theme->set_font_size("bold_size", "EditorFonts", default_font_size);
p_theme->set_font("bold", "EditorFonts", df_bold);
@ -430,7 +309,8 @@ void editor_register_fonts(Ref<Theme> p_theme) {
p_theme->set_font_size("font_size", "HeaderLarge", default_font_size + 3 * EDSCALE);
// Documentation fonts
MAKE_SOURCE_FONT(df_code);
String code_font_custom_variations = EditorSettings::get_singleton()->get("interface/editor/code_font_custom_variations");
MAKE_SOURCE_FONT(df_code, code_font_custom_variations, default_font_size);
p_theme->set_font_size("doc_size", "EditorFonts", int(EDITOR_GET("text_editor/help/help_font_size")) * EDSCALE);
p_theme->set_font("doc", "EditorFonts", df);
p_theme->set_font_size("doc_bold_size", "EditorFonts", int(EDITOR_GET("text_editor/help/help_font_size")) * EDSCALE);

View File

@ -97,10 +97,14 @@
#include "editor/editor_translation_parser.h"
#include "editor/export_template_manager.h"
#include "editor/filesystem_dock.h"
#include "editor/import/dynamicfont_import_settings.h"
#include "editor/import/editor_import_collada.h"
#include "editor/import/resource_importer_bitmask.h"
#include "editor/import/resource_importer_bmfont.h"
#include "editor/import/resource_importer_csv_translation.h"
#include "editor/import/resource_importer_dynamicfont.h"
#include "editor/import/resource_importer_image.h"
#include "editor/import/resource_importer_imagefont.h"
#include "editor/import/resource_importer_layered_texture.h"
#include "editor/import/resource_importer_obj.h"
#include "editor/import/resource_importer_scene.h"
@ -5853,6 +5857,18 @@ EditorNode::EditorNode() {
import_texture_atlas.instantiate();
ResourceFormatImporter::get_singleton()->add_importer(import_texture_atlas);
Ref<ResourceImporterDynamicFont> import_font_data_dynamic;
import_font_data_dynamic.instantiate();
ResourceFormatImporter::get_singleton()->add_importer(import_font_data_dynamic);
Ref<ResourceImporterBMFont> import_font_data_bmfont;
import_font_data_bmfont.instantiate();
ResourceFormatImporter::get_singleton()->add_importer(import_font_data_bmfont);
Ref<ResourceImporterImageFont> import_font_data_image;
import_font_data_image.instantiate();
ResourceFormatImporter::get_singleton()->add_importer(import_font_data_image);
Ref<ResourceImporterCSVTranslation> import_csv_translation;
import_csv_translation.instantiate();
ResourceFormatImporter::get_singleton()->add_importer(import_csv_translation);
@ -6258,6 +6274,9 @@ EditorNode::EditorNode() {
scene_import_settings = memnew(SceneImportSettings);
gui_base->add_child(scene_import_settings);
fontdata_import_settings = memnew(DynamicFontImportSettings);
gui_base->add_child(fontdata_import_settings);
export_template_manager = memnew(ExportTemplateManager);
gui_base->add_child(export_template_manager);

View File

@ -93,6 +93,7 @@ class Window;
class SubViewport;
class SceneImportSettings;
class EditorExtensionManager;
class DynamicFontImportSettings;
class EditorNode : public Node {
GDCLASS(EditorNode, Node);
@ -422,6 +423,7 @@ private:
EditorResourcePreview *resource_preview;
EditorFolding editor_folding;
DynamicFontImportSettings *fontdata_import_settings;
SceneImportSettings *scene_import_settings;
struct BottomPanelItem {
String name;

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,167 @@
/*************************************************************************/
/* dynamicfont_import_settings.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* 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. */
/*************************************************************************/
#ifndef FONTDATA_IMPORT_SETTINGS_H
#define FONTDATA_IMPORT_SETTINGS_H
#include "editor/editor_file_dialog.h"
#include "editor/editor_inspector.h"
#include "editor/import/resource_importer_dynamicfont.h"
#include "scene/gui/dialogs.h"
#include "scene/gui/item_list.h"
#include "scene/gui/option_button.h"
#include "scene/gui/split_container.h"
#include "scene/gui/subviewport_container.h"
#include "scene/gui/tab_container.h"
#include "scene/gui/text_edit.h"
#include "scene/gui/tree.h"
#include "scene/resources/font.h"
#include "servers/text_server.h"
class DynamicFontImportSettingsData;
class DynamicFontImportSettings : public ConfirmationDialog {
GDCLASS(DynamicFontImportSettings, ConfirmationDialog)
friend class DynamicFontImportSettingsData;
enum ItemButton {
BUTTON_ADD_VAR,
BUTTON_REMOVE_VAR,
};
static DynamicFontImportSettings *singleton;
String base_path;
Ref<DynamicFontImportSettingsData> import_settings_data;
List<ResourceImporter::ImportOption> options_variations;
List<ResourceImporter::ImportOption> options_general;
// Root layout
Label *label_warn = nullptr;
TabContainer *main_pages = nullptr;
// Page 1 layout: Rendering Options
Label *page1_description = nullptr;
Label *font_preview_label = nullptr;
EditorInspector *inspector_general = nullptr;
void _main_prop_changed(const String &p_edited_property);
// Page 2 layout: Configurations
Label *page2_description = nullptr;
Label *label_vars = nullptr;
Button *add_var = nullptr;
Tree *vars_list = nullptr;
TreeItem *vars_list_root = nullptr;
EditorInspector *inspector_vars = nullptr;
void _variation_add();
void _variation_selected();
void _variation_remove(Object *p_item, int p_column, int p_id);
void _variation_changed(const String &p_edited_property);
void _variations_validate();
// Page 3 layout: Text to select glyphs
Label *page3_description = nullptr;
Label *label_glyphs = nullptr;
TextEdit *text_edit = nullptr;
LineEdit *ftr_edit = nullptr;
LineEdit *lang_edit = nullptr;
void _change_text_opts();
void _glyph_text_selected();
void _glyph_clear();
// Page 4 layout: Character map
Label *page4_description = nullptr;
Tree *glyph_table = nullptr;
Tree *glyph_tree = nullptr;
TreeItem *glyph_root = nullptr;
void _glyph_selected();
void _range_edited();
void _range_selected();
void _edit_range(int32_t p_start, int32_t p_end);
bool _char_update(int32_t p_char);
void _range_update(int32_t p_start, int32_t p_end);
// Page 5 layout: Metadata override
Label *page5_description = nullptr;
Button *add_lang = nullptr;
Button *add_script = nullptr;
PopupMenu *menu_langs = nullptr;
PopupMenu *menu_scripts = nullptr;
Tree *lang_list = nullptr;
TreeItem *lang_list_root = nullptr;
Tree *script_list = nullptr;
TreeItem *script_list_root = nullptr;
Label *label_langs = nullptr;
Label *label_script = nullptr;
void _lang_add();
void _lang_add_item(int p_option);
void _lang_remove(Object *p_item, int p_column, int p_id);
void _script_add();
void _script_add_item(int p_option);
void _script_remove(Object *p_item, int p_column, int p_id);
// Common
void _add_glyph_range_item(int32_t p_start, int32_t p_end, const String &p_name);
Ref<Font> font_preview;
Ref<Font> font_main;
Set<char32_t> selected_chars;
Set<int32_t> selected_glyphs;
void _re_import();
String _pad_zeros(const String &p_hex) const;
protected:
void _notification(int p_what);
public:
void open_settings(const String &p_path);
static DynamicFontImportSettings *get_singleton();
DynamicFontImportSettings();
};
#endif // FONTDATA_IMPORT_SETTINGS_H

View File

@ -0,0 +1,797 @@
/*************************************************************************/
/* resource_importer_bmfont.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* 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. */
/*************************************************************************/
#include "resource_importer_bmfont.h"
#include "core/io/image_loader.h"
#include "core/io/resource_saver.h"
String ResourceImporterBMFont::get_importer_name() const {
return "font_data_bmfont";
}
String ResourceImporterBMFont::get_visible_name() const {
return "Font Data (AngelCode BMFont)";
}
void ResourceImporterBMFont::get_recognized_extensions(List<String> *p_extensions) const {
if (p_extensions) {
p_extensions->push_back("font");
p_extensions->push_back("fnt");
}
}
String ResourceImporterBMFont::get_save_extension() const {
return "fontdata";
}
String ResourceImporterBMFont::get_resource_type() const {
return "FontData";
}
bool ResourceImporterBMFont::get_option_visibility(const String &p_option, const Map<StringName, Variant> &p_options) const {
return true;
}
void ResourceImporterBMFont::get_import_options(List<ImportOption> *r_options, int p_preset) const {
r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "compress"), true));
}
void _convert_packed_8bit(Ref<FontData> &r_font, Ref<Image> &p_source, int p_page, int p_sz) {
int w = p_source->get_width();
int h = p_source->get_height();
PackedByteArray imgdata = p_source->get_data();
const uint8_t *r = imgdata.ptr();
PackedByteArray imgdata_r;
imgdata_r.resize(w * h * 2);
uint8_t *wr = imgdata_r.ptrw();
PackedByteArray imgdata_g;
imgdata_g.resize(w * h * 2);
uint8_t *wg = imgdata_g.ptrw();
PackedByteArray imgdata_b;
imgdata_b.resize(w * h * 2);
uint8_t *wb = imgdata_b.ptrw();
PackedByteArray imgdata_a;
imgdata_a.resize(w * h * 2);
uint8_t *wa = imgdata_a.ptrw();
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
int ofs_src = (i * w + j) * 4;
int ofs_dst = (i * w + j) * 2;
wr[ofs_dst + 0] = 255;
wr[ofs_dst + 1] = r[ofs_src + 0];
wg[ofs_dst + 0] = 255;
wg[ofs_dst + 1] = r[ofs_src + 1];
wb[ofs_dst + 0] = 255;
wb[ofs_dst + 1] = r[ofs_src + 2];
wa[ofs_dst + 0] = 255;
wa[ofs_dst + 1] = r[ofs_src + 3];
}
}
Ref<Image> img_r = memnew(Image(w, h, 0, Image::FORMAT_LA8, imgdata_r));
r_font->set_texture_image(0, Vector2i(p_sz, 0), p_page * 4 + 0, img_r);
Ref<Image> img_g = memnew(Image(w, h, 0, Image::FORMAT_LA8, imgdata_g));
r_font->set_texture_image(0, Vector2i(p_sz, 0), p_page * 4 + 1, img_g);
Ref<Image> img_b = memnew(Image(w, h, 0, Image::FORMAT_LA8, imgdata_b));
r_font->set_texture_image(0, Vector2i(p_sz, 0), p_page * 4 + 2, img_b);
Ref<Image> img_a = memnew(Image(w, h, 0, Image::FORMAT_LA8, imgdata_a));
r_font->set_texture_image(0, Vector2i(p_sz, 0), p_page * 4 + 3, img_a);
}
void _convert_packed_4bit(Ref<FontData> &r_font, Ref<Image> &p_source, int p_page, int p_sz) {
int w = p_source->get_width();
int h = p_source->get_height();
PackedByteArray imgdata = p_source->get_data();
const uint8_t *r = imgdata.ptr();
PackedByteArray imgdata_r;
imgdata_r.resize(w * h * 2);
uint8_t *wr = imgdata_r.ptrw();
PackedByteArray imgdata_g;
imgdata_g.resize(w * h * 2);
uint8_t *wg = imgdata_g.ptrw();
PackedByteArray imgdata_b;
imgdata_b.resize(w * h * 2);
uint8_t *wb = imgdata_b.ptrw();
PackedByteArray imgdata_a;
imgdata_a.resize(w * h * 2);
uint8_t *wa = imgdata_a.ptrw();
PackedByteArray imgdata_ro;
imgdata_ro.resize(w * h * 2);
uint8_t *wro = imgdata_ro.ptrw();
PackedByteArray imgdata_go;
imgdata_go.resize(w * h * 2);
uint8_t *wgo = imgdata_go.ptrw();
PackedByteArray imgdata_bo;
imgdata_bo.resize(w * h * 2);
uint8_t *wbo = imgdata_bo.ptrw();
PackedByteArray imgdata_ao;
imgdata_ao.resize(w * h * 2);
uint8_t *wao = imgdata_ao.ptrw();
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
int ofs_src = (i * w + j) * 4;
int ofs_dst = (i * w + j) * 2;
wr[ofs_dst + 0] = 255;
wro[ofs_dst + 0] = 255;
if (r[ofs_src + 0] > 0x0F) {
wr[ofs_dst + 1] = (r[ofs_src + 0] - 0x0F) * 2;
wro[ofs_dst + 1] = 0;
} else {
wr[ofs_dst + 1] = 0;
wro[ofs_dst + 1] = r[ofs_src + 0] * 2;
}
wg[ofs_dst + 0] = 255;
wgo[ofs_dst + 0] = 255;
if (r[ofs_src + 1] > 0x0F) {
wg[ofs_dst + 1] = (r[ofs_src + 1] - 0x0F) * 2;
wgo[ofs_dst + 1] = 0;
} else {
wg[ofs_dst + 1] = 0;
wgo[ofs_dst + 1] = r[ofs_src + 1] * 2;
}
wb[ofs_dst + 0] = 255;
wbo[ofs_dst + 0] = 255;
if (r[ofs_src + 2] > 0x0F) {
wb[ofs_dst + 1] = (r[ofs_src + 2] - 0x0F) * 2;
wbo[ofs_dst + 1] = 0;
} else {
wb[ofs_dst + 1] = 0;
wbo[ofs_dst + 1] = r[ofs_src + 2] * 2;
}
wa[ofs_dst + 0] = 255;
wao[ofs_dst + 0] = 255;
if (r[ofs_src + 3] > 0x0F) {
wa[ofs_dst + 1] = (r[ofs_src + 3] - 0x0F) * 2;
wao[ofs_dst + 1] = 0;
} else {
wa[ofs_dst + 1] = 0;
wao[ofs_dst + 1] = r[ofs_src + 3] * 2;
}
}
}
Ref<Image> img_r = memnew(Image(w, h, 0, Image::FORMAT_LA8, imgdata_r));
r_font->set_texture_image(0, Vector2i(p_sz, 0), p_page * 4 + 0, img_r);
Ref<Image> img_g = memnew(Image(w, h, 0, Image::FORMAT_LA8, imgdata_g));
r_font->set_texture_image(0, Vector2i(p_sz, 0), p_page * 4 + 1, img_g);
Ref<Image> img_b = memnew(Image(w, h, 0, Image::FORMAT_LA8, imgdata_b));
r_font->set_texture_image(0, Vector2i(p_sz, 0), p_page * 4 + 2, img_b);
Ref<Image> img_a = memnew(Image(w, h, 0, Image::FORMAT_LA8, imgdata_a));
r_font->set_texture_image(0, Vector2i(p_sz, 0), p_page * 4 + 3, img_a);
Ref<Image> img_ro = memnew(Image(w, h, 0, Image::FORMAT_LA8, imgdata_ro));
r_font->set_texture_image(0, Vector2i(p_sz, 1), p_page * 4 + 0, img_ro);
Ref<Image> img_go = memnew(Image(w, h, 0, Image::FORMAT_LA8, imgdata_go));
r_font->set_texture_image(0, Vector2i(p_sz, 1), p_page * 4 + 1, img_go);
Ref<Image> img_bo = memnew(Image(w, h, 0, Image::FORMAT_LA8, imgdata_bo));
r_font->set_texture_image(0, Vector2i(p_sz, 1), p_page * 4 + 2, img_bo);
Ref<Image> img_ao = memnew(Image(w, h, 0, Image::FORMAT_LA8, imgdata_ao));
r_font->set_texture_image(0, Vector2i(p_sz, 1), p_page * 4 + 3, img_ao);
}
void _convert_rgba_4bit(Ref<FontData> &r_font, Ref<Image> &p_source, int p_page, int p_sz) {
int w = p_source->get_width();
int h = p_source->get_height();
PackedByteArray imgdata = p_source->get_data();
const uint8_t *r = imgdata.ptr();
PackedByteArray imgdata_g;
imgdata_g.resize(w * h * 4);
uint8_t *wg = imgdata_g.ptrw();
PackedByteArray imgdata_o;
imgdata_o.resize(w * h * 4);
uint8_t *wo = imgdata_o.ptrw();
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
int ofs = (i * w + j) * 4;
if (r[ofs + 0] > 0x7F) {
wg[ofs + 0] = r[ofs + 0];
wo[ofs + 0] = 0;
} else {
wg[ofs + 0] = 0;
wo[ofs + 0] = r[ofs + 0] * 2;
}
if (r[ofs + 1] > 0x7F) {
wg[ofs + 1] = r[ofs + 1];
wo[ofs + 1] = 0;
} else {
wg[ofs + 1] = 0;
wo[ofs + 1] = r[ofs + 1] * 2;
}
if (r[ofs + 2] > 0x7F) {
wg[ofs + 2] = r[ofs + 2];
wo[ofs + 2] = 0;
} else {
wg[ofs + 2] = 0;
wo[ofs + 2] = r[ofs + 2] * 2;
}
if (r[ofs + 3] > 0x7F) {
wg[ofs + 3] = r[ofs + 3];
wo[ofs + 3] = 0;
} else {
wg[ofs + 3] = 0;
wo[ofs + 3] = r[ofs + 3] * 2;
}
}
}
Ref<Image> img_g = memnew(Image(w, h, 0, Image::FORMAT_RGBA8, imgdata_g));
r_font->set_texture_image(0, Vector2i(p_sz, 0), p_page, img_g);
Ref<Image> img_o = memnew(Image(w, h, 0, Image::FORMAT_RGBA8, imgdata_o));
r_font->set_texture_image(0, Vector2i(p_sz, 1), p_page, img_o);
}
void _convert_mono_8bit(Ref<FontData> &r_font, Ref<Image> &p_source, int p_page, int p_ch, int p_sz, int p_ol) {
int w = p_source->get_width();
int h = p_source->get_height();
PackedByteArray imgdata = p_source->get_data();
const uint8_t *r = imgdata.ptr();
int size = 4;
if (p_source->get_format() == Image::FORMAT_L8) {
size = 1;
p_ch = 0;
}
PackedByteArray imgdata_g;
imgdata_g.resize(w * h * 2);
uint8_t *wg = imgdata_g.ptrw();
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
int ofs_src = (i * w + j) * size;
int ofs_dst = (i * w + j) * 2;
wg[ofs_dst + 0] = 255;
wg[ofs_dst + 1] = r[ofs_src + p_ch];
}
}
Ref<Image> img_g = memnew(Image(w, h, 0, Image::FORMAT_LA8, imgdata_g));
r_font->set_texture_image(0, Vector2i(p_sz, p_ol), p_page, img_g);
}
void _convert_mono_4bit(Ref<FontData> &r_font, Ref<Image> &p_source, int p_page, int p_ch, int p_sz, int p_ol) {
int w = p_source->get_width();
int h = p_source->get_height();
PackedByteArray imgdata = p_source->get_data();
const uint8_t *r = imgdata.ptr();
int size = 4;
if (p_source->get_format() == Image::FORMAT_L8) {
size = 1;
p_ch = 0;
}
PackedByteArray imgdata_g;
imgdata_g.resize(w * h * 2);
uint8_t *wg = imgdata_g.ptrw();
PackedByteArray imgdata_o;
imgdata_o.resize(w * h * 2);
uint8_t *wo = imgdata_o.ptrw();
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
int ofs_src = (i * w + j) * size;
int ofs_dst = (i * w + j) * 2;
wg[ofs_dst + 0] = 255;
wo[ofs_dst + 0] = 255;
if (r[ofs_src + p_ch] > 0x7F) {
wg[ofs_dst + 1] = r[ofs_src + p_ch];
wo[ofs_dst + 1] = 0;
} else {
wg[ofs_dst + 1] = 0;
wo[ofs_dst + 1] = r[ofs_src + p_ch] * 2;
}
}
}
Ref<Image> img_g = memnew(Image(w, h, 0, Image::FORMAT_LA8, imgdata_g));
r_font->set_texture_image(0, Vector2i(p_sz, 0), p_page, img_g);
Ref<Image> img_o = memnew(Image(w, h, 0, Image::FORMAT_LA8, imgdata_o));
r_font->set_texture_image(0, Vector2i(p_sz, p_ol), p_page, img_o);
}
Error ResourceImporterBMFont::import(const String &p_source_file, const String &p_save_path, const Map<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files, Variant *r_metadata) {
print_verbose("Importing BMFont font from: " + p_source_file);
Ref<FontData> font;
font.instantiate();
font->set_antialiased(false);
font->set_multichannel_signed_distance_field(false);
font->set_force_autohinter(false);
font->set_hinting(TextServer::HINTING_NONE);
font->set_oversampling(1.0f);
FileAccessRef f = FileAccess::open(p_source_file, FileAccess::READ);
if (f == nullptr) {
ERR_FAIL_V_MSG(ERR_CANT_CREATE, TTR("Cannot open font from file ") + "\"" + p_source_file + "\".");
}
int base_size = 16;
int height = 0;
int ascent = 0;
int outline = 0;
bool packed = false;
uint8_t ch[4] = { 0, 0, 0, 0 }; // RGBA
int first_gl_ch = -1;
int first_ol_ch = -1;
int first_cm_ch = -1;
unsigned char magic[4];
f->get_buffer((unsigned char *)&magic, 4);
if (magic[0] == 'B' && magic[1] == 'M' && magic[2] == 'F') {
// Binary BMFont file.
ERR_FAIL_COND_V_MSG(magic[3] != 3, ERR_CANT_CREATE, vformat(TTR("Version %d of BMFont is not supported."), (int)magic[3]));
uint8_t block_type = f->get_8();
uint32_t block_size = f->get_32();
while (!f->eof_reached()) {
uint64_t off = f->get_position();
switch (block_type) {
case 1: /* info */ {
ERR_FAIL_COND_V_MSG(block_size < 15, ERR_CANT_CREATE, TTR("Invalid BMFont info block size."));
base_size = f->get_16();
uint8_t flags = f->get_8();
ERR_FAIL_COND_V_MSG(flags & 0x02, ERR_CANT_CREATE, TTR("Non-unicode version of BMFont is not supported."));
f->get_8(); // non-unicode charset, skip
f->get_16(); // stretch_h, skip
f->get_8(); // aa, skip
f->get_32(); // padding, skip
f->get_16(); // spacing, skip
outline = f->get_8();
// font name, skip
font->set_fixed_size(base_size);
} break;
case 2: /* common */ {
ERR_FAIL_COND_V_MSG(block_size != 15, ERR_CANT_CREATE, TTR("Invalid BMFont common block size."));
height = f->get_16();
ascent = f->get_16();
f->get_32(); // scale, skip
f->get_16(); // pages, skip
uint8_t flags = f->get_8();
packed = (flags & 0x01);
ch[3] = f->get_8();
ch[0] = f->get_8();
ch[1] = f->get_8();
ch[2] = f->get_8();
for (int i = 0; i < 4; i++) {
if (ch[i] == 0 && first_gl_ch == -1) {
first_gl_ch = i;
}
if (ch[i] == 1 && first_ol_ch == -1) {
first_ol_ch = i;
}
if (ch[i] == 2 && first_cm_ch == -1) {
first_cm_ch = i;
}
}
} break;
case 3: /* pages */ {
int page = 0;
CharString cs;
char32_t c = f->get_8();
while (!f->eof_reached() && f->get_position() <= off + block_size) {
if (c == '\0') {
String base_dir = p_source_file.get_base_dir();
String file = base_dir.plus_file(String::utf8(cs.ptr(), cs.length()));
if (RenderingServer::get_singleton() != nullptr) {
Ref<Image> img;
img.instantiate();
Error err = ImageLoader::load_image(file, img);
ERR_FAIL_COND_V_MSG(err != OK, ERR_FILE_CANT_READ, TTR("Can't load font texture: ") + "\"" + file + "\".");
if (packed) {
if (ch[3] == 0) { // 4 x 8 bit monochrome, no outline
outline = 0;
ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8, ERR_FILE_CANT_READ, TTR("Unsupported BMFont texture format."));
_convert_packed_8bit(font, img, page, base_size);
} else if ((ch[3] == 2) && (outline > 0)) { // 4 x 4 bit monochrome, gl + outline
ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8, ERR_FILE_CANT_READ, TTR("Unsupported BMFont texture format."));
_convert_packed_4bit(font, img, page, base_size);
} else {
ERR_FAIL_V_MSG(ERR_CANT_CREATE, TTR("Unsupported BMFont texture format."));
}
} else {
if ((ch[0] == 0) && (ch[1] == 0) && (ch[2] == 0) && (ch[3] == 0)) { // RGBA8 color, no outline
outline = 0;
ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8, ERR_FILE_CANT_READ, TTR("Unsupported BMFont texture format."));
font->set_texture_image(0, Vector2i(base_size, 0), page, img);
} else if ((ch[0] == 2) && (ch[1] == 2) && (ch[2] == 2) && (ch[3] == 2) && (outline > 0)) { // RGBA4 color, gl + outline
ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8, ERR_FILE_CANT_READ, TTR("Unsupported BMFont texture format."));
_convert_rgba_4bit(font, img, page, base_size);
} else if ((first_gl_ch >= 0) && (first_ol_ch >= 0) && (outline > 0)) { // 1 x 8 bit monochrome, gl + outline
ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8 && img->get_format() != Image::FORMAT_L8, ERR_FILE_CANT_READ, TTR("Unsupported BMFont texture format."));
_convert_mono_8bit(font, img, page, first_gl_ch, base_size, 0);
_convert_mono_8bit(font, img, page, first_ol_ch, base_size, 1);
} else if ((first_cm_ch >= 0) && (outline > 0)) { // 1 x 4 bit monochrome, gl + outline
ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8 && img->get_format() != Image::FORMAT_L8, ERR_FILE_CANT_READ, TTR("Unsupported BMFont texture format."));
_convert_mono_4bit(font, img, page, first_cm_ch, base_size, 1);
} else if (first_gl_ch >= 0) { // 1 x 8 bit monochrome, no outline
outline = 0;
ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8 && img->get_format() != Image::FORMAT_L8, ERR_FILE_CANT_READ, TTR("Unsupported BMFont texture format."));
_convert_mono_8bit(font, img, page, first_gl_ch, base_size, 0);
} else {
ERR_FAIL_V_MSG(ERR_CANT_CREATE, TTR("Unsupported BMFont texture format."));
}
}
}
page++;
cs = "";
} else {
cs += c;
}
c = f->get_8();
}
} break;
case 4: /* chars */ {
int char_count = block_size / 20;
for (int i = 0; i < char_count; i++) {
Vector2 advance;
Vector2 size;
Vector2 offset;
Rect2 uv_rect;
char32_t idx = f->get_32();
uv_rect.position.x = (int16_t)f->get_16();
uv_rect.position.y = (int16_t)f->get_16();
uv_rect.size.width = (int16_t)f->get_16();
size.width = uv_rect.size.width;
uv_rect.size.height = (int16_t)f->get_16();
size.height = uv_rect.size.height;
offset.x = (int16_t)f->get_16();
offset.y = (int16_t)f->get_16() - ascent;
advance.x = (int16_t)f->get_16();
if (advance.x < 0) {
advance.x = size.width + 1;
}
int texture_idx = f->get_8();
uint8_t channel = f->get_8();
ERR_FAIL_COND_V_MSG(!packed && channel != 15, ERR_CANT_CREATE, TTR("Invalid glyph channel."));
int ch_off = 0;
switch (channel) {
case 1:
ch_off = 2;
break; // B
case 2:
ch_off = 1;
break; // G
case 4:
ch_off = 0;
break; // R
case 8:
ch_off = 3;
break; // A
default:
ch_off = 0;
break;
}
font->set_glyph_advance(0, base_size, idx, advance);
font->set_glyph_offset(0, Vector2i(base_size, 0), idx, offset);
font->set_glyph_size(0, Vector2i(base_size, 0), idx, size);
font->set_glyph_uv_rect(0, Vector2i(base_size, 0), idx, uv_rect);
font->set_glyph_texture_idx(0, Vector2i(base_size, 0), idx, texture_idx * (packed ? 4 : 1) + ch_off);
if (outline > 0) {
font->set_glyph_offset(0, Vector2i(base_size, 1), idx, offset);
font->set_glyph_size(0, Vector2i(base_size, 1), idx, size);
font->set_glyph_uv_rect(0, Vector2i(base_size, 1), idx, uv_rect);
font->set_glyph_texture_idx(0, Vector2i(base_size, 1), idx, texture_idx * (packed ? 4 : 1) + ch_off);
}
}
} break;
case 5: /* kerning */ {
int pair_count = block_size / 10;
for (int i = 0; i < pair_count; i++) {
Vector2i kpk;
kpk.x = f->get_32();
kpk.y = f->get_32();
font->set_kerning(0, base_size, kpk, Vector2((int16_t)f->get_16(), 0));
}
} break;
default: {
ERR_FAIL_V_MSG(ERR_CANT_CREATE, TTR("Invalid BMFont block type."));
} break;
}
f->seek(off + block_size);
block_type = f->get_8();
block_size = f->get_32();
}
} else {
// Text BMFont file.
f->seek(0);
while (true) {
String line = f->get_line();
int delimiter = line.find(" ");
String type = line.substr(0, delimiter);
int pos = delimiter + 1;
Map<String, String> keys;
while (pos < line.size() && line[pos] == ' ') {
pos++;
}
while (pos < line.size()) {
int eq = line.find("=", pos);
if (eq == -1) {
break;
}
String key = line.substr(pos, eq - pos);
int end = -1;
String value;
if (line[eq + 1] == '"') {
end = line.find("\"", eq + 2);
if (end == -1) {
break;
}
value = line.substr(eq + 2, end - 1 - eq - 1);
pos = end + 1;
} else {
end = line.find(" ", eq + 1);
if (end == -1) {
end = line.size();
}
value = line.substr(eq + 1, end - eq);
pos = end;
}
while (pos < line.size() && line[pos] == ' ') {
pos++;
}
keys[key] = value;
}
if (type == "info") {
if (keys.has("size")) {
base_size = keys["size"].to_int();
font->set_fixed_size(base_size);
}
if (keys.has("outline")) {
outline = keys["outline"].to_int();
}
ERR_FAIL_COND_V_MSG((!keys.has("unicode") || keys["unicode"].to_int() != 1), ERR_CANT_CREATE, TTR("Non-unicode version of BMFont is not supported."));
} else if (type == "common") {
if (keys.has("lineHeight")) {
height = keys["lineHeight"].to_int();
}
if (keys.has("base")) {
ascent = keys["base"].to_int();
}
if (keys.has("packed")) {
packed = (keys["packed"].to_int() == 1);
}
if (keys.has("alphaChnl")) {
ch[3] = keys["alphaChnl"].to_int();
}
if (keys.has("redChnl")) {
ch[0] = keys["redChnl"].to_int();
}
if (keys.has("greenChnl")) {
ch[1] = keys["greenChnl"].to_int();
}
if (keys.has("blueChnl")) {
ch[2] = keys["blueChnl"].to_int();
}
for (int i = 0; i < 4; i++) {
if (ch[i] == 0 && first_gl_ch == -1) {
first_gl_ch = i;
}
if (ch[i] == 1 && first_ol_ch == -1) {
first_ol_ch = i;
}
if (ch[i] == 2 && first_cm_ch == -1) {
first_cm_ch = i;
}
}
} else if (type == "page") {
int page = 0;
if (keys.has("id")) {
page = keys["id"].to_int();
}
if (keys.has("file")) {
String base_dir = p_source_file.get_base_dir();
String file = base_dir.plus_file(keys["file"]);
if (RenderingServer::get_singleton() != nullptr) {
Ref<Image> img;
img.instantiate();
Error err = ImageLoader::load_image(file, img);
ERR_FAIL_COND_V_MSG(err != OK, ERR_FILE_CANT_READ, TTR("Can't load font texture: ") + "\"" + file + "\".");
if (packed) {
if (ch[3] == 0) { // 4 x 8 bit monochrome, no outline
outline = 0;
ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8, ERR_FILE_CANT_READ, TTR("Unsupported BMFont texture format."));
_convert_packed_8bit(font, img, page, base_size);
} else if ((ch[3] == 2) && (outline > 0)) { // 4 x 4 bit monochrome, gl + outline
ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8, ERR_FILE_CANT_READ, TTR("Unsupported BMFont texture format."));
_convert_packed_4bit(font, img, page, base_size);
} else {
ERR_FAIL_V_MSG(ERR_CANT_CREATE, TTR("Unsupported BMFont texture format."));
}
} else {
if ((ch[0] == 0) && (ch[1] == 0) && (ch[2] == 0) && (ch[3] == 0)) { // RGBA8 color, no outline
outline = 0;
ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8, ERR_FILE_CANT_READ, TTR("Unsupported BMFont texture format."));
font->set_texture_image(0, Vector2i(base_size, 0), page, img);
} else if ((ch[0] == 2) && (ch[1] == 2) && (ch[2] == 2) && (ch[3] == 2) && (outline > 0)) { // RGBA4 color, gl + outline
ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8, ERR_FILE_CANT_READ, TTR("Unsupported BMFont texture format."));
_convert_rgba_4bit(font, img, page, base_size);
} else if ((first_gl_ch >= 0) && (first_ol_ch >= 0) && (outline > 0)) { // 1 x 8 bit monochrome, gl + outline
ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8 && img->get_format() != Image::FORMAT_L8, ERR_FILE_CANT_READ, TTR("Unsupported BMFont texture format."));
_convert_mono_8bit(font, img, page, first_gl_ch, base_size, 0);
_convert_mono_8bit(font, img, page, first_ol_ch, base_size, 1);
} else if ((first_cm_ch >= 0) && (outline > 0)) { // 1 x 4 bit monochrome, gl + outline
ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8 && img->get_format() != Image::FORMAT_L8, ERR_FILE_CANT_READ, TTR("Unsupported BMFont texture format."));
_convert_mono_4bit(font, img, page, first_cm_ch, base_size, 1);
} else if (first_gl_ch >= 0) { // 1 x 8 bit monochrome, no outline
outline = 0;
ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8 && img->get_format() != Image::FORMAT_L8, ERR_FILE_CANT_READ, TTR("Unsupported BMFont texture format."));
_convert_mono_8bit(font, img, page, first_gl_ch, base_size, 0);
} else {
ERR_FAIL_V_MSG(ERR_CANT_CREATE, TTR("Unsupported BMFont texture format."));
}
}
}
}
} else if (type == "char") {
char32_t idx = 0;
Vector2 advance;
Vector2 size;
Vector2 offset;
Rect2 uv_rect;
int texture_idx = -1;
uint8_t channel = 15;
if (keys.has("id")) {
idx = keys["id"].to_int();
}
if (keys.has("x")) {
uv_rect.position.x = keys["x"].to_int();
}
if (keys.has("y")) {
uv_rect.position.y = keys["y"].to_int();
}
if (keys.has("width")) {
uv_rect.size.width = keys["width"].to_int();
size.width = keys["width"].to_int();
}
if (keys.has("height")) {
uv_rect.size.height = keys["height"].to_int();
size.height = keys["height"].to_int();
}
if (keys.has("xoffset")) {
offset.x = keys["xoffset"].to_int();
}
if (keys.has("yoffset")) {
offset.y = keys["yoffset"].to_int() - ascent;
}
if (keys.has("page")) {
texture_idx = keys["page"].to_int();
}
if (keys.has("xadvance")) {
advance.x = keys["xadvance"].to_int();
}
if (advance.x < 0) {
advance.x = size.width + 1;
}
if (keys.has("chnl")) {
channel = keys["chnl"].to_int();
}
ERR_FAIL_COND_V_MSG(!packed && channel != 15, ERR_CANT_CREATE, TTR("Invalid glyph channel."));
int ch_off = 0;
switch (channel) {
case 1:
ch_off = 2;
break; // B
case 2:
ch_off = 1;
break; // G
case 4:
ch_off = 0;
break; // R
case 8:
ch_off = 3;
break; // A
default:
ch_off = 0;
break;
}
font->set_glyph_advance(0, base_size, idx, advance);
font->set_glyph_offset(0, Vector2i(base_size, 0), idx, offset);
font->set_glyph_size(0, Vector2i(base_size, 0), idx, size);
font->set_glyph_uv_rect(0, Vector2i(base_size, 0), idx, uv_rect);
font->set_glyph_texture_idx(0, Vector2i(base_size, 0), idx, texture_idx * (packed ? 4 : 1) + ch_off);
if (outline > 0) {
font->set_glyph_offset(0, Vector2i(base_size, 1), idx, offset);
font->set_glyph_size(0, Vector2i(base_size, 1), idx, size);
font->set_glyph_uv_rect(0, Vector2i(base_size, 1), idx, uv_rect);
font->set_glyph_texture_idx(0, Vector2i(base_size, 1), idx, texture_idx * (packed ? 4 : 1) + ch_off);
}
} else if (type == "kerning") {
Vector2i kpk;
if (keys.has("first")) {
kpk.x = keys["first"].to_int();
}
if (keys.has("second")) {
kpk.y = keys["second"].to_int();
}
if (keys.has("amount")) {
font->set_kerning(0, base_size, kpk, Vector2(keys["amount"].to_int(), 0));
}
}
if (f->eof_reached()) {
break;
}
}
}
font->set_ascent(0, base_size, ascent);
font->set_descent(0, base_size, height - ascent);
int flg = ResourceSaver::SaverFlags::FLAG_BUNDLE_RESOURCES | ResourceSaver::FLAG_REPLACE_SUBRESOURCE_PATHS;
if ((bool)p_options["compress"]) {
flg |= ResourceSaver::SaverFlags::FLAG_COMPRESS;
}
print_verbose("Saving to: " + p_save_path + ".fontdata");
Error err = ResourceSaver::save(p_save_path + ".fontdata", font, flg);
ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot save font to file \"" + p_save_path + ".res\".");
print_verbose("Done saving to: " + p_save_path + ".fontdata");
return OK;
}
ResourceImporterBMFont::ResourceImporterBMFont() {
}

View File

@ -0,0 +1,56 @@
/*************************************************************************/
/* resource_importer_bmfont.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* 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. */
/*************************************************************************/
#ifndef RESOURCE_IMPORTER_BMFONT_H
#define RESOURCE_IMPORTER_BMFONT_H
#include "core/io/resource_importer.h"
#include "scene/resources/font.h"
#include "servers/text_server.h"
class ResourceImporterBMFont : public ResourceImporter {
GDCLASS(ResourceImporterBMFont, ResourceImporter);
public:
virtual String get_importer_name() const override;
virtual String get_visible_name() const override;
virtual void get_recognized_extensions(List<String> *p_extensions) const override;
virtual String get_save_extension() const override;
virtual String get_resource_type() const override;
virtual void get_import_options(List<ImportOption> *r_options, int p_preset = 0) const override;
virtual bool get_option_visibility(const String &p_option, const Map<StringName, Variant> &p_options) const override;
virtual Error import(const String &p_source_file, const String &p_save_path, const Map<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files = nullptr, Variant *r_metadata = nullptr) override;
ResourceImporterBMFont();
};
#endif // RESOURCE_IMPORTER_BMFONT_H

View File

@ -0,0 +1,304 @@
/*************************************************************************/
/* resource_importer_dynamicfont.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* 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. */
/*************************************************************************/
#include "resource_importer_dynamicfont.h"
#include "dynamicfont_import_settings.h"
#include "core/io/file_access.h"
#include "core/io/resource_saver.h"
#include "editor/editor_node.h"
#include "modules/modules_enabled.gen.h"
String ResourceImporterDynamicFont::get_importer_name() const {
return "font_data_dynamic";
}
String ResourceImporterDynamicFont::get_visible_name() const {
return "Font Data (Dynamic Font)";
}
void ResourceImporterDynamicFont::get_recognized_extensions(List<String> *p_extensions) const {
if (p_extensions) {
#ifdef MODULE_FREETYPE_ENABLED
p_extensions->push_back("ttf");
p_extensions->push_back("otf");
p_extensions->push_back("woff");
//p_extensions->push_back("woff2");
p_extensions->push_back("pfb");
p_extensions->push_back("pfm");
#endif
}
}
String ResourceImporterDynamicFont::get_save_extension() const {
return "fontdata";
}
String ResourceImporterDynamicFont::get_resource_type() const {
return "FontData";
}
bool ResourceImporterDynamicFont::get_option_visibility(const String &p_option, const Map<StringName, Variant> &p_options) const {
if (p_option == "msdf_pixel_range" && !bool(p_options["multichannel_signed_distance_field"])) {
return false;
}
if (p_option == "msdf_size" && !bool(p_options["multichannel_signed_distance_field"])) {
return false;
}
if (p_option == "oversampling" && bool(p_options["multichannel_signed_distance_field"])) {
return false;
}
return true;
}
int ResourceImporterDynamicFont::get_preset_count() const {
return PRESET_MAX;
}
String ResourceImporterDynamicFont::get_preset_name(int p_idx) const {
switch (p_idx) {
case PRESET_DYNAMIC:
return TTR("Dynamically rendered TrueType/OpenType font");
case PRESET_MSDF:
return TTR("Prerendered multichannel(+true) signed distance field");
default:
return String();
}
}
void ResourceImporterDynamicFont::get_import_options(List<ImportOption> *r_options, int p_preset) const {
bool msdf = p_preset == PRESET_MSDF;
r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "antialiased"), true));
r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "multichannel_signed_distance_field", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), (msdf) ? true : false));
r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "msdf_pixel_range", PROPERTY_HINT_RANGE, "1,100,1"), 8));
r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "msdf_size", PROPERTY_HINT_RANGE, "1,250,1"), 48));
r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "force_autohinter"), false));
r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "hinting", PROPERTY_HINT_ENUM, "None,Light,Normal"), 1));
r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "oversampling", PROPERTY_HINT_RANGE, "0,10,0.1"), 0.0));
r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "compress"), true));
r_options->push_back(ImportOption(PropertyInfo(Variant::PACKED_STRING_ARRAY, "preload/char_ranges"), Vector<String>()));
r_options->push_back(ImportOption(PropertyInfo(Variant::PACKED_STRING_ARRAY, "preload/glyph_ranges"), Vector<String>()));
r_options->push_back(ImportOption(PropertyInfo(Variant::PACKED_STRING_ARRAY, "preload/configurations"), Vector<String>()));
r_options->push_back(ImportOption(PropertyInfo(Variant::PACKED_STRING_ARRAY, "support_overrides/language_enabled"), Vector<String>()));
r_options->push_back(ImportOption(PropertyInfo(Variant::PACKED_STRING_ARRAY, "support_overrides/language_disabled"), Vector<String>()));
r_options->push_back(ImportOption(PropertyInfo(Variant::PACKED_STRING_ARRAY, "support_overrides/script_enabled"), Vector<String>()));
r_options->push_back(ImportOption(PropertyInfo(Variant::PACKED_STRING_ARRAY, "support_overrides/script_disabled"), Vector<String>()));
}
bool ResourceImporterDynamicFont::_decode_variation(const String &p_token, Dictionary &r_variations, Vector2i &r_size, String &r_name, Vector2i &r_spacing) {
Vector<String> tokens = p_token.split("=");
if (tokens.size() == 2) {
if (tokens[0] == "name") {
r_name = tokens[1];
} else if (tokens[0] == "size") {
r_size.x = tokens[1].to_int();
} else if (tokens[0] == "outline_size") {
r_size.y = tokens[1].to_int();
} else if (tokens[0] == "spacing_space") {
r_spacing.x = tokens[1].to_int();
} else if (tokens[0] == "spacing_glyph") {
r_spacing.y = tokens[1].to_int();
} else {
r_variations[tokens[0]] = tokens[1].to_float();
}
return true;
} else {
WARN_PRINT("Invalid variation: '" + p_token + "'.");
return false;
}
}
bool ResourceImporterDynamicFont::_decode_range(const String &p_token, int32_t &r_pos) {
if (p_token.begins_with("U+") || p_token.begins_with("u+") || p_token.begins_with("0x")) {
// Unicode character hex index.
r_pos = p_token.substr(2).hex_to_int();
return true;
} else if (p_token.length() == 3 && p_token[0] == '\'' && p_token[2] == '\'') {
// Unicode character.
r_pos = p_token.unicode_at(1);
return true;
} else if (p_token.is_numeric()) {
// Unicode character decimal index.
r_pos = p_token.to_int();
return true;
} else {
return false;
}
}
bool ResourceImporterDynamicFont::has_advanced_options() const {
return true;
}
void ResourceImporterDynamicFont::show_advanced_options(const String &p_path) {
DynamicFontImportSettings::get_singleton()->open_settings(p_path);
}
Error ResourceImporterDynamicFont::import(const String &p_source_file, const String &p_save_path, const Map<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files, Variant *r_metadata) {
print_verbose("Importing dynamic font from: " + p_source_file);
bool antialiased = p_options["antialiased"];
bool msdf = p_options["multichannel_signed_distance_field"];
int px_range = p_options["msdf_pixel_range"];
int px_size = p_options["msdf_size"];
bool autohinter = p_options["force_autohinter"];
int hinting = p_options["hinting"];
real_t oversampling = p_options["oversampling"];
// Load base font data.
Vector<uint8_t> data = FileAccess::get_file_as_array(p_source_file);
// Create font.
Ref<FontData> font;
font.instantiate();
font->set_data(data);
font->set_antialiased(antialiased);
font->set_multichannel_signed_distance_field(msdf);
font->set_msdf_pixel_range(px_range);
font->set_msdf_size(px_size);
font->set_fixed_size(0);
font->set_force_autohinter(autohinter);
font->set_hinting((TextServer::Hinting)hinting);
font->set_oversampling(oversampling);
Vector<String> lang_en = p_options["support_overrides/language_enabled"];
for (int i = 0; i < lang_en.size(); i++) {
font->set_language_support_override(lang_en[i], true);
}
Vector<String> lang_dis = p_options["support_overrides/language_disabled"];
for (int i = 0; i < lang_dis.size(); i++) {
font->set_language_support_override(lang_dis[i], false);
}
Vector<String> scr_en = p_options["support_overrides/script_enabled"];
for (int i = 0; i < scr_en.size(); i++) {
font->set_script_support_override(scr_en[i], true);
}
Vector<String> scr_dis = p_options["support_overrides/script_disabled"];
for (int i = 0; i < scr_dis.size(); i++) {
font->set_script_support_override(scr_dis[i], false);
}
Vector<String> variations = p_options["preload/configurations"];
Vector<String> char_ranges = p_options["preload/char_ranges"];
Vector<String> gl_ranges = p_options["preload/glyph_ranges"];
for (int i = 0; i < variations.size(); i++) {
String name;
Dictionary var;
Vector2i size = Vector2(16, 0);
Vector2i spacing;
Vector<String> variation_tags = variations[i].split(",");
for (int j = 0; j < variation_tags.size(); j++) {
if (!_decode_variation(variation_tags[j], var, size, name, spacing)) {
WARN_PRINT(vformat(TTR("Invalid variation: \"%s\""), variations[i]));
continue;
}
}
RID conf = font->find_cache(var);
for (int j = 0; j < char_ranges.size(); j++) {
int32_t start, end;
Vector<String> tokens = char_ranges[j].split("-");
if (tokens.size() == 2) {
if (!_decode_range(tokens[0], start) || !_decode_range(tokens[1], end)) {
WARN_PRINT(vformat(TTR("Invalid range: \"%s\""), char_ranges[j]));
continue;
}
} else if (tokens.size() == 1) {
if (!_decode_range(tokens[0], start)) {
WARN_PRINT(vformat(TTR("Invalid range: \"%s\""), char_ranges[j]));
continue;
}
end = start;
} else {
WARN_PRINT(vformat(TTR("Invalid range: \"%s\""), char_ranges[j]));
continue;
}
// Preload character ranges for each variations / sizes.
print_verbose(vformat(TTR("Pre-rendering range U+%s...%s from configuration \"%s\" (%d / %d)..."), String::num_int64(start, 16), String::num_int64(end, 16), name, i + 1, variations.size()));
TS->font_render_range(conf, size, start, end);
}
for (int j = 0; j < gl_ranges.size(); j++) {
int32_t start, end;
Vector<String> tokens = gl_ranges[j].split("-");
if (tokens.size() == 2) {
if (!_decode_range(tokens[0], start) || !_decode_range(tokens[1], end)) {
WARN_PRINT(vformat(TTR("Invalid range: \"%s\""), gl_ranges[j]));
continue;
}
} else if (tokens.size() == 1) {
if (!_decode_range(tokens[0], start)) {
WARN_PRINT(vformat(TTR("Invalid range: \"%s\""), gl_ranges[j]));
continue;
}
end = start;
} else {
WARN_PRINT(vformat(TTR("Invalid range: \"%s\""), gl_ranges[j]));
continue;
}
// Preload glyph range for each variations / sizes.
print_verbose(vformat(TTR("Pre-rendering glyph range 0x%s...%s from configuration \"%s\" (%d / %d)..."), String::num_int64(start, 16), String::num_int64(end, 16), name, i + 1, variations.size()));
for (int32_t k = start; k <= end; k++) {
TS->font_render_glyph(conf, size, k);
}
}
TS->font_set_spacing(conf, size.x, TextServer::SPACING_SPACE, spacing.x);
TS->font_set_spacing(conf, size.x, TextServer::SPACING_GLYPH, spacing.y);
}
int flg = ResourceSaver::SaverFlags::FLAG_BUNDLE_RESOURCES | ResourceSaver::FLAG_REPLACE_SUBRESOURCE_PATHS;
if ((bool)p_options["compress"]) {
flg |= ResourceSaver::SaverFlags::FLAG_COMPRESS;
}
print_verbose("Saving to: " + p_save_path + ".fontdata");
Error err = ResourceSaver::save(p_save_path + ".fontdata", font, flg);
ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot save font to file \"" + p_save_path + ".res\".");
print_verbose("Done saving to: " + p_save_path + ".fontdata");
return OK;
}
ResourceImporterDynamicFont::ResourceImporterDynamicFont() {
}

View File

@ -0,0 +1,71 @@
/*************************************************************************/
/* resource_importer_dynamicfont.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* 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. */
/*************************************************************************/
#ifndef RESOURCE_IMPORTER_FONT_DATA_H
#define RESOURCE_IMPORTER_FONT_DATA_H
#include "core/io/resource_importer.h"
#include "scene/resources/font.h"
#include "servers/text_server.h"
class ResourceImporterDynamicFont : public ResourceImporter {
GDCLASS(ResourceImporterDynamicFont, ResourceImporter);
enum Presets {
PRESET_DYNAMIC,
PRESET_MSDF,
PRESET_MAX
};
public:
static bool _decode_range(const String &p_token, int32_t &r_pos);
static bool _decode_variation(const String &p_token, Dictionary &r_variations, Vector2i &r_size, String &r_name, Vector2i &r_spacing);
virtual String get_importer_name() const override;
virtual String get_visible_name() const override;
virtual void get_recognized_extensions(List<String> *p_extensions) const override;
virtual String get_save_extension() const override;
virtual String get_resource_type() const override;
virtual int get_preset_count() const override;
virtual String get_preset_name(int p_idx) const override;
virtual void get_import_options(List<ImportOption> *r_options, int p_preset = 0) const override;
virtual bool get_option_visibility(const String &p_option, const Map<StringName, Variant> &p_options) const override;
bool has_advanced_options() const override;
void show_advanced_options(const String &p_path) override;
virtual Error import(const String &p_source_file, const String &p_save_path, const Map<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files = nullptr, Variant *r_metadata = nullptr) override;
ResourceImporterDynamicFont();
};
#endif // RESOURCE_IMPORTER_FONTDATA_H

View File

@ -0,0 +1,162 @@
/*************************************************************************/
/* resource_importer_imagefont.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* 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. */
/*************************************************************************/
#include "resource_importer_imagefont.h"
#include "core/io/image_loader.h"
#include "core/io/resource_saver.h"
String ResourceImporterImageFont::get_importer_name() const {
return "font_data_image";
}
String ResourceImporterImageFont::get_visible_name() const {
return "Font Data (Monospace Image Font)";
}
void ResourceImporterImageFont::get_recognized_extensions(List<String> *p_extensions) const {
if (p_extensions) {
ImageLoader::get_recognized_extensions(p_extensions);
}
}
String ResourceImporterImageFont::get_save_extension() const {
return "fontdata";
}
String ResourceImporterImageFont::get_resource_type() const {
return "FontData";
}
bool ResourceImporterImageFont::get_option_visibility(const String &p_option, const Map<StringName, Variant> &p_options) const {
return true;
}
void ResourceImporterImageFont::get_import_options(List<ImportOption> *r_options, int p_preset) const {
r_options->push_back(ImportOption(PropertyInfo(Variant::PACKED_STRING_ARRAY, "character_ranges"), Vector<String>()));
r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "columns"), 1));
r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "rows"), 1));
r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "font_size"), 14));
r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "compress"), true));
}
bool ResourceImporterImageFont::_decode_range(const String &p_token, int32_t &r_pos) {
if (p_token.begins_with("U+") || p_token.begins_with("u+") || p_token.begins_with("0x")) {
// Unicode character hex index.
r_pos = p_token.substr(2).hex_to_int();
return true;
} else if (p_token.length() == 3 && p_token[0] == '\'' && p_token[2] == '\'') {
// Unicode character.
r_pos = p_token.unicode_at(1);
return true;
} else if (p_token.is_numeric()) {
// Unicode character decimal index.
r_pos = p_token.to_int();
return true;
} else {
return false;
}
}
Error ResourceImporterImageFont::import(const String &p_source_file, const String &p_save_path, const Map<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files, Variant *r_metadata) {
print_verbose("Importing image font from: " + p_source_file);
int columns = p_options["columns"];
int rows = p_options["rows"];
int base_size = p_options["font_size"];
Vector<String> ranges = p_options["character_ranges"];
Ref<FontData> font;
font.instantiate();
font->set_antialiased(false);
font->set_multichannel_signed_distance_field(false);
font->set_fixed_size(base_size);
font->set_force_autohinter(false);
font->set_hinting(TextServer::HINTING_NONE);
font->set_oversampling(1.0f);
Ref<Image> img;
img.instantiate();
Error err = ImageLoader::load_image(p_source_file, img);
ERR_FAIL_COND_V_MSG(err != OK, ERR_FILE_CANT_READ, TTR("Can't load font texture: ") + "\"" + p_source_file + "\".");
font->set_texture_image(0, Vector2i(base_size, 0), 0, img);
int count = columns * rows;
int chr_width = img->get_width() / columns;
int chr_height = img->get_height() / rows;
int pos = 0;
for (int i = 0; i < ranges.size(); i++) {
int32_t start, end;
Vector<String> tokens = ranges[i].split("-");
if (tokens.size() == 2) {
if (!_decode_range(tokens[0], start) || !_decode_range(tokens[1], end)) {
WARN_PRINT("Invalid range: \"" + ranges[i] + "\"");
continue;
}
} else if (tokens.size() == 1) {
if (!_decode_range(tokens[0], start)) {
WARN_PRINT("Invalid range: \"" + ranges[i] + "\"");
continue;
}
end = start;
} else {
WARN_PRINT("Invalid range: \"" + ranges[i] + "\"");
continue;
}
for (int32_t idx = start; idx <= end; idx++) {
int x = pos % columns;
int y = pos / columns;
font->set_glyph_advance(0, base_size, idx, Vector2(chr_width, 0));
font->set_glyph_offset(0, Vector2i(base_size, 0), idx, Vector2(0, -0.5 * chr_height));
font->set_glyph_size(0, Vector2i(base_size, 0), idx, Vector2(chr_width, chr_height));
font->set_glyph_uv_rect(0, Vector2i(base_size, 0), idx, Rect2(chr_width * x, chr_height * y, chr_width, chr_height));
font->set_glyph_texture_idx(0, Vector2i(base_size, 0), idx, 0);
pos++;
ERR_FAIL_COND_V_MSG(pos >= count, ERR_CANT_CREATE, "Too many characters in range.");
}
}
font->set_ascent(0, base_size, 0.5 * chr_height);
font->set_descent(0, base_size, 0.5 * chr_height);
int flg = ResourceSaver::SaverFlags::FLAG_BUNDLE_RESOURCES | ResourceSaver::FLAG_REPLACE_SUBRESOURCE_PATHS;
if ((bool)p_options["compress"]) {
flg |= ResourceSaver::SaverFlags::FLAG_COMPRESS;
}
print_verbose("Saving to: " + p_save_path + ".fontdata");
err = ResourceSaver::save(p_save_path + ".fontdata", font, flg);
ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot save font to file \"" + p_save_path + ".res\".");
print_verbose("Done saving to: " + p_save_path + ".fontdata");
return OK;
}
ResourceImporterImageFont::ResourceImporterImageFont() {
}

View File

@ -0,0 +1,58 @@
/*************************************************************************/
/* resource_importer_imagefont.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* 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. */
/*************************************************************************/
#ifndef RESOURCE_IMPORTER_IMAGE_FONT_H
#define RESOURCE_IMPORTER_IMAGE_FONT_H
#include "core/io/resource_importer.h"
#include "scene/resources/font.h"
#include "servers/text_server.h"
class ResourceImporterImageFont : public ResourceImporter {
GDCLASS(ResourceImporterImageFont, ResourceImporter);
public:
static bool _decode_range(const String &p_token, int32_t &r_pos);
virtual String get_importer_name() const override;
virtual String get_visible_name() const override;
virtual void get_recognized_extensions(List<String> *p_extensions) const override;
virtual String get_save_extension() const override;
virtual String get_resource_type() const override;
virtual void get_import_options(List<ImportOption> *r_options, int p_preset = 0) const override;
virtual bool get_option_visibility(const String &p_option, const Map<StringName, Variant> &p_options) const override;
virtual Error import(const String &p_source_file, const String &p_save_path, const Map<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files = nullptr, Variant *r_metadata = nullptr) override;
ResourceImporterImageFont();
};
#endif // RESOURCE_IMPORTER_IMAGE_FONT_H

View File

@ -826,55 +826,6 @@ bool EditorFontPreviewPlugin::handles(const String &p_type) const {
return ClassDB::is_parent_class(p_type, "FontData") || ClassDB::is_parent_class(p_type, "Font");
}
struct FSample {
String script;
String sample;
};
static FSample _samples[] = {
{ "hani", U"漢字" },
{ "armn", U"Աբ" },
{ "copt", U"Αα" },
{ "cyrl", U"Аб" },
{ "grek", U"Αα" },
{ "hebr", U"אב" },
{ "arab", U"اب" },
{ "syrc", U"ܐܒ" },
{ "thaa", U"ހށ" },
{ "deva", U"" },
{ "beng", U"" },
{ "guru", U"" },
{ "gujr", U"" },
{ "orya", U"" },
{ "taml", U"" },
{ "telu", U"" },
{ "knda", U"" },
{ "mylm", U"" },
{ "sinh", U"" },
{ "thai", U"กิ" },
{ "laoo", U"ກິ" },
{ "tibt", U"" },
{ "mymr", U"က" },
{ "geor", U"Ⴀა" },
{ "hang", U"한글" },
{ "ethi", U"" },
{ "cher", U"" },
{ "cans", U"" },
{ "ogam", U"" },
{ "runr", U"" },
{ "tglg", U"" },
{ "hano", U"" },
{ "buhd", U"" },
{ "tagb", U"" },
{ "khmr", U"" },
{ "mong", U"" },
{ "limb", U"" },
{ "tale", U"" },
{ "latn", U"Ab" },
{ "zyyy", U"😀" },
{ "", U"" }
};
Ref<Texture2D> EditorFontPreviewPlugin::generate_from_path(const String &p_path, const Size2 &p_size) const {
RES res = ResourceLoader::load(p_path);
Ref<Font> sampled_font;
@ -886,15 +837,15 @@ Ref<Texture2D> EditorFontPreviewPlugin::generate_from_path(const String &p_path,
}
String sample;
for (int j = 0; j < sampled_font->get_data_count(); j++) {
for (int i = 0; _samples[i].script != String(); i++) {
if (sampled_font->get_data(j)->is_script_supported(_samples[i].script)) {
if (sampled_font->get_data(j)->has_char(_samples[i].sample[0])) {
sample += _samples[i].sample;
}
}
static const String sample_base = U"12漢字ԱբΑαАбΑαאבابܐܒހށआআਆઆଆஆఆಆആආกิກິༀကႠა한글ᎣᐁᚁᚠᜀᜠᝀᝠកᠠᤁᥐAb😀";
for (int i = 0; i < sample_base.length(); i++) {
if (sampled_font->has_char(sample_base[i])) {
sample += sample_base[i];
}
}
if (sample.is_empty()) {
sample = sampled_font->get_supported_chars().substr(0, 6);
}
Vector2 size = sampled_font->get_string_size(sample, 50);
Vector2 pos;

View File

@ -50,70 +50,24 @@ Size2 FontDataPreview::get_minimum_size() const {
return Vector2(64, 64) * EDSCALE;
}
struct FSample {
String script;
String sample;
};
static FSample _samples[] = {
{ "hani", U"漢字" },
{ "armn", U"Աբ" },
{ "copt", U"Αα" },
{ "cyrl", U"Аб" },
{ "grek", U"Αα" },
{ "hebr", U"אב" },
{ "arab", U"اب" },
{ "syrc", U"ܐܒ" },
{ "thaa", U"ހށ" },
{ "deva", U"" },
{ "beng", U"" },
{ "guru", U"" },
{ "gujr", U"" },
{ "orya", U"" },
{ "taml", U"" },
{ "telu", U"" },
{ "knda", U"" },
{ "mylm", U"" },
{ "sinh", U"" },
{ "thai", U"กิ" },
{ "laoo", U"ກິ" },
{ "tibt", U"" },
{ "mymr", U"က" },
{ "geor", U"Ⴀა" },
{ "hang", U"한글" },
{ "ethi", U"" },
{ "cher", U"" },
{ "cans", U"" },
{ "ogam", U"" },
{ "runr", U"" },
{ "tglg", U"" },
{ "hano", U"" },
{ "buhd", U"" },
{ "tagb", U"" },
{ "khmr", U"" },
{ "mong", U"" },
{ "limb", U"" },
{ "tale", U"" },
{ "latn", U"Ab" },
{ "zyyy", U"😀" },
{ "", U"" }
};
void FontDataPreview::set_data(const Ref<FontData> &p_data) {
Ref<Font> f = memnew(Font);
f->add_data(p_data);
line->clear();
String sample;
for (int i = 0; _samples[i].script != String(); i++) {
if (p_data->is_script_supported(_samples[i].script)) {
if (p_data->has_char(_samples[i].sample[0])) {
sample += _samples[i].sample;
if (p_data.is_valid()) {
String sample;
static const String sample_base = U"12漢字ԱբΑαАбΑαאבابܐܒހށआআਆઆଆஆఆಆആආกิກິༀကႠა한글ᎣᐁᚁᚠᜀᜠᝀᝠកᠠᤁᥐAb😀";
for (int i = 0; i < sample_base.length(); i++) {
if (p_data->has_char(sample_base[i])) {
sample += sample_base[i];
}
}
if (sample.is_empty()) {
sample = p_data->get_supported_chars().substr(0, 6);
}
line->add_string(sample, f, 72);
}
line->add_string(sample, f, 72);
update();
}
@ -124,159 +78,6 @@ FontDataPreview::FontDataPreview() {
/*************************************************************************/
void FontDataEditor::_notification(int p_what) {
if (p_what == NOTIFICATION_SORT_CHILDREN) {
int split_width = get_name_split_ratio() * get_size().width;
button->set_size(Size2(get_theme_icon(SNAME("Add"), SNAME("EditorIcons"))->get_width(), get_size().height));
if (is_layout_rtl()) {
if (le != nullptr) {
fit_child_in_rect(le, Rect2(Vector2(split_width, 0), Size2(split_width, get_size().height)));
}
fit_child_in_rect(chk, Rect2(Vector2(split_width - chk->get_size().x, 0), Size2(chk->get_size().x, get_size().height)));
fit_child_in_rect(button, Rect2(Vector2(0, 0), Size2(button->get_size().width, get_size().height)));
} else {
if (le != nullptr) {
fit_child_in_rect(le, Rect2(Vector2(0, 0), Size2(split_width, get_size().height)));
}
fit_child_in_rect(chk, Rect2(Vector2(split_width, 0), Size2(chk->get_size().x, get_size().height)));
fit_child_in_rect(button, Rect2(Vector2(get_size().width - button->get_size().width, 0), Size2(button->get_size().width, get_size().height)));
}
update();
}
if (p_what == NOTIFICATION_DRAW) {
int split_width = get_name_split_ratio() * get_size().width;
Color dark_color = get_theme_color(SNAME("dark_color_2"), SNAME("Editor"));
if (is_layout_rtl()) {
draw_rect(Rect2(Vector2(0, 0), Size2(split_width, get_size().height)), dark_color);
} else {
draw_rect(Rect2(Vector2(split_width, 0), Size2(split_width, get_size().height)), dark_color);
}
}
if (p_what == NOTIFICATION_THEME_CHANGED) {
if (le != nullptr) {
button->set_icon(get_theme_icon(SNAME("Add"), SNAME("EditorIcons")));
} else {
button->set_icon(get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")));
}
queue_sort();
}
if (p_what == NOTIFICATION_RESIZED) {
queue_sort();
}
}
void FontDataEditor::update_property() {
if (le == nullptr) {
bool c = get_edited_object()->get(get_edited_property());
chk->set_pressed(c);
chk->set_disabled(is_read_only());
}
}
Size2 FontDataEditor::get_minimum_size() const {
return Size2(0, 60);
}
void FontDataEditor::_bind_methods() {
}
void FontDataEditor::init_lang_add() {
le = memnew(LineEdit);
le->set_placeholder("Language code");
le->set_custom_minimum_size(Size2(get_size().width / 2, 0));
le->set_editable(true);
add_child(le);
button->set_icon(get_theme_icon(SNAME("Add"), SNAME("EditorIcons")));
button->connect("pressed", callable_mp(this, &FontDataEditor::add_lang));
}
void FontDataEditor::init_lang_edit() {
button->set_icon(get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")));
button->connect("pressed", callable_mp(this, &FontDataEditor::remove_lang));
chk->connect("toggled", callable_mp(this, &FontDataEditor::toggle_lang));
}
void FontDataEditor::init_script_add() {
le = memnew(LineEdit);
le->set_placeholder("Script code");
le->set_custom_minimum_size(Size2(get_size().width / 2, 0));
le->set_editable(true);
add_child(le);
button->set_icon(get_theme_icon(SNAME("Add"), SNAME("EditorIcons")));
button->connect("pressed", callable_mp(this, &FontDataEditor::add_script));
}
void FontDataEditor::init_script_edit() {
button->set_icon(get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")));
button->connect("pressed", callable_mp(this, &FontDataEditor::remove_script));
chk->connect("toggled", callable_mp(this, &FontDataEditor::toggle_script));
}
void FontDataEditor::add_lang() {
FontData *fd = Object::cast_to<FontData>(get_edited_object());
if (fd != nullptr && !le->get_text().is_empty()) {
fd->set_language_support_override(le->get_text(), chk->is_pressed());
le->set_text("");
chk->set_pressed(false);
}
}
void FontDataEditor::add_script() {
FontData *fd = Object::cast_to<FontData>(get_edited_object());
if (fd != nullptr && le->get_text().length() == 4) {
fd->set_script_support_override(le->get_text(), chk->is_pressed());
le->set_text("");
chk->set_pressed(false);
}
}
void FontDataEditor::toggle_lang(bool p_pressed) {
FontData *fd = Object::cast_to<FontData>(get_edited_object());
if (fd != nullptr) {
String lang = String(get_edited_property()).replace("language_support_override/", "");
fd->set_language_support_override(lang, p_pressed);
}
}
void FontDataEditor::toggle_script(bool p_pressed) {
FontData *fd = Object::cast_to<FontData>(get_edited_object());
if (fd != nullptr) {
String script = String(get_edited_property()).replace("script_support_override/", "");
fd->set_script_support_override(script, p_pressed);
}
}
void FontDataEditor::remove_lang() {
FontData *fd = Object::cast_to<FontData>(get_edited_object());
if (fd != nullptr) {
String lang = String(get_edited_property()).replace("language_support_override/", "");
fd->remove_language_support_override(lang);
}
}
void FontDataEditor::remove_script() {
FontData *fd = Object::cast_to<FontData>(get_edited_object());
if (fd != nullptr) {
String script = String(get_edited_property()).replace("script_support_override/", "");
fd->remove_script_support_override(script);
}
}
FontDataEditor::FontDataEditor() {
chk = memnew(CheckBox);
chk->set_text(TTR("On"));
chk->set_flat(true);
add_child(chk);
button = memnew(Button);
button->set_flat(true);
add_child(button);
}
/*************************************************************************/
bool EditorInspectorPluginFont::can_handle(Object *p_object) {
return Object::cast_to<FontData>(p_object) != nullptr;
}
@ -291,34 +92,6 @@ void EditorInspectorPluginFont::parse_begin(Object *p_object) {
}
bool EditorInspectorPluginFont::parse_property(Object *p_object, const Variant::Type p_type, const String &p_path, const PropertyHint p_hint, const String &p_hint_text, const uint32_t p_usage, const bool p_wide) {
if (p_path.begins_with("language_support_override/") && p_object->is_class("FontData")) {
String lang = p_path.replace("language_support_override/", "");
FontDataEditor *editor = memnew(FontDataEditor);
if (lang != "_new") {
editor->init_lang_edit();
} else {
editor->init_lang_add();
}
add_property_editor(p_path, editor);
return true;
}
if (p_path.begins_with("script_support_override/") && p_object->is_class("FontData")) {
String script = p_path.replace("script_support_override/", "");
FontDataEditor *editor = memnew(FontDataEditor);
if (script != "_new") {
editor->init_script_edit();
} else {
editor->init_script_add();
}
add_property_editor(p_path, editor);
return true;
}
return false;
}

View File

@ -55,39 +55,6 @@ public:
/*************************************************************************/
class FontDataEditor : public EditorProperty {
GDCLASS(FontDataEditor, EditorProperty);
LineEdit *le = nullptr;
CheckBox *chk = nullptr;
Button *button = nullptr;
void toggle_lang(bool p_pressed);
void toggle_script(bool p_pressed);
void add_lang();
void add_script();
void remove_lang();
void remove_script();
protected:
void _notification(int p_what);
static void _bind_methods();
public:
virtual Size2 get_minimum_size() const override;
virtual void update_property() override;
void init_lang_add();
void init_lang_edit();
void init_script_add();
void init_script_edit();
FontDataEditor();
};
/*************************************************************************/
class EditorInspectorPluginFont : public EditorInspectorPlugin {
GDCLASS(EditorInspectorPluginFont, EditorInspectorPlugin);

View File

@ -319,7 +319,7 @@ def disable_module(self):
self.disabled_modules.append(self.current_module)
def module_check_dependencies(self, module, dependencies):
def module_check_dependencies(self, module, dependencies, silent=False):
"""
Checks if module dependencies are enabled for a given module,
and prints a warning if they aren't.
@ -333,11 +333,12 @@ def module_check_dependencies(self, module, dependencies):
missing_deps.append(dep)
if missing_deps != []:
print(
"Disabling '{}' module as the following dependencies are not satisfied: {}".format(
module, ", ".join(missing_deps)
if not silent:
print(
"Disabling '{}' module as the following dependencies are not satisfied: {}".format(
module, ", ".join(missing_deps)
)
)
)
return False
else:
return True

View File

@ -5354,7 +5354,7 @@
},
{
"name": "godot_glyph_get_advance",
"return_type": "godot_float",
"return_type": "godot_real_t",
"arguments": [
[
"const godot_glyph *",
@ -5371,7 +5371,7 @@
"p_self"
],
[
"godot_float",
"godot_real_t",
"p_advance"
]
]

View File

@ -60,68 +60,109 @@ typedef struct {
typedef struct {
godot_gdnative_api_version version;
void *(*constructor)(godot_object *);
void (*destructor)(void *);
godot_string (*get_name)(const void *);
godot_bool (*has_feature)(const void *, godot_int);
void (*free)(void *, godot_rid *);
bool (*has)(void *, godot_rid *);
bool (*load_support_data)(void *, const godot_string *);
godot_string (*get_support_data_filename)(const void *);
godot_string (*get_support_data_info)(const void *);
bool (*save_support_data)(void *, const godot_string *);
bool (*is_locale_right_to_left)(void *, const godot_string *);
void (*free)(void *, godot_rid *);
bool (*has)(void *, godot_rid *);
godot_rid (*create_font_system)(void *, const godot_string *, int);
godot_rid (*create_font_resource)(void *, const godot_string *, int);
godot_rid (*create_font_memory)(void *, const uint8_t *, size_t, godot_string *, int);
godot_rid (*create_font_bitmap)(void *, float, float, int);
void (*font_bitmap_add_texture)(void *, godot_rid *, const godot_object *);
void (*font_bitmap_add_char)(void *, godot_rid *, char32_t, int, const godot_rect2 *, const godot_vector2 *, float);
void (*font_bitmap_add_kerning_pair)(void *, godot_rid *, char32_t, char32_t, int);
float (*font_get_height)(void *, godot_rid *, int);
float (*font_get_ascent)(void *, godot_rid *, int);
float (*font_get_descent)(void *, godot_rid *, int);
float (*font_get_underline_position)(void *, godot_rid *, int);
float (*font_get_underline_thickness)(void *, godot_rid *, int);
int (*font_get_spacing_space)(void *, godot_rid *);
void (*font_set_spacing_space)(void *, godot_rid *, int);
int (*font_get_spacing_glyph)(void *, godot_rid *);
void (*font_set_spacing_glyph)(void *, godot_rid *, int);
int32_t (*name_to_tag)(const void *, const godot_string *);
godot_string (*tag_to_name)(const void *, int32_t);
godot_rid (*create_font)(void *);
void (*font_set_data)(void *, godot_rid *, const godot_packed_byte_array *);
void (*font_set_data_ptr)(void *, godot_rid *, const uint8_t *, size_t);
void (*font_set_antialiased)(void *, godot_rid *, bool);
bool (*font_get_antialiased)(void *, godot_rid *);
godot_dictionary (*font_get_feature_list)(void *, godot_rid *);
godot_dictionary (*font_get_variation_list)(void *, godot_rid *);
void (*font_set_variation)(void *, godot_rid *, const godot_string *, double);
double (*font_get_variation)(void *, godot_rid *, const godot_string *);
void (*font_set_distance_field_hint)(void *, godot_rid *, bool);
bool (*font_get_distance_field_hint)(void *, godot_rid *);
void (*font_set_hinting)(void *, godot_rid *, godot_int);
godot_int (*font_get_hinting)(void *, godot_rid *);
bool (*font_is_antialiased)(const void *, godot_rid *);
void (*font_set_multichannel_signed_distance_field)(void *, godot_rid *, bool);
bool (*font_is_multichannel_signed_distance_field)(const void *, godot_rid *);
void (*font_set_msdf_pixel_range)(void *, godot_rid *, godot_int);
godot_int (*font_get_msdf_pixel_range)(const void *, godot_rid *);
void (*font_set_msdf_size)(void *, godot_rid *, godot_int);
godot_int (*font_get_msdf_size)(const void *, godot_rid *);
void (*font_set_fixed_size)(void *, godot_rid *, godot_int);
godot_int (*font_get_fixed_size)(const void *, godot_rid *);
void (*font_set_force_autohinter)(void *, godot_rid *, bool);
bool (*font_get_force_autohinter)(void *, godot_rid *);
bool (*font_has_char)(void *, godot_rid *, char32_t);
godot_string (*font_get_supported_chars)(void *, godot_rid *);
bool (*font_has_outline)(void *, godot_rid *);
int (*font_get_base_size)(void *, godot_rid *);
bool (*font_is_language_supported)(void *, godot_rid *, const godot_string *);
bool (*font_is_force_autohinter)(const void *, godot_rid *);
void (*font_set_hinting)(void *, godot_rid *, godot_int);
godot_int (*font_get_hinting)(const void *, godot_rid *);
void (*font_set_variation_coordinates)(void *, godot_rid *, const godot_dictionary *);
godot_dictionary (*font_get_variation_coordinates)(const void *, godot_rid *);
void (*font_set_oversampling)(void *, godot_rid *, godot_real_t);
godot_real_t (*font_get_oversampling)(const void *, godot_rid *);
godot_array (*font_get_size_cache_list)(const void *, godot_rid *);
void (*font_clear_size_cache)(void *, godot_rid *);
void (*font_remove_size_cache)(void *, godot_rid *, const godot_vector2i *);
void (*font_set_ascent)(void *, godot_rid *, godot_int, godot_real_t);
godot_real_t (*font_get_ascent)(const void *, godot_rid *, godot_int);
void (*font_set_descent)(void *, godot_rid *, godot_int, godot_real_t);
godot_real_t (*font_get_descent)(const void *, godot_rid *, godot_int);
void (*font_set_underline_position)(void *, godot_rid *, godot_int, godot_real_t);
godot_real_t (*font_get_underline_position)(const void *, godot_rid *, godot_int);
void (*font_set_underline_thickness)(void *, godot_rid *, godot_int, godot_real_t);
godot_real_t (*font_get_underline_thickness)(const void *, godot_rid *, godot_int);
void (*font_set_scale)(void *, godot_rid *, godot_int, godot_real_t);
godot_real_t (*font_get_scale)(const void *, godot_rid *, godot_int);
void (*font_set_spacing)(void *, godot_rid *, godot_int, godot_int, godot_int);
godot_int (*font_get_spacing)(const void *, godot_rid *, godot_int, godot_int);
godot_int (*font_get_texture_count)(const void *, godot_rid *, const godot_vector2i *);
void (*font_clear_textures)(void *, godot_rid *, const godot_vector2i *);
void (*font_remove_texture)(void *, godot_rid *, const godot_vector2i *, godot_int);
void (*font_set_texture_image)(void *, godot_rid *, const godot_vector2i *, godot_int, const godot_object *);
godot_object *(*font_get_texture_image)(const void *, godot_rid *, const godot_vector2i *, godot_int);
void (*font_set_texture_offsets)(void *, godot_rid *, const godot_vector2i *, godot_int, const godot_packed_int32_array *);
godot_packed_int32_array (*font_get_texture_offsets)(const void *, godot_rid *, const godot_vector2i *, godot_int);
godot_array (*font_get_glyph_list)(const void *, godot_rid *, const godot_vector2i *);
void (*font_clear_glyphs)(void *, godot_rid *, const godot_vector2i *);
void (*font_remove_glyph)(void *, godot_rid *, const godot_vector2i *, int32_t);
godot_vector2 (*font_get_glyph_advance)(const void *, godot_rid *, godot_int, int32_t);
void (*font_set_glyph_advance)(void *, godot_rid *, godot_int, int32_t, const godot_vector2 *);
godot_vector2 (*font_get_glyph_offset)(const void *, godot_rid *, const godot_vector2i *, int32_t);
void (*font_set_glyph_offset)(void *, godot_rid *, const godot_vector2i *, int32_t, const godot_vector2 *);
godot_vector2 (*font_get_glyph_size)(const void *, godot_rid *, const godot_vector2i *, int32_t);
void (*font_set_glyph_size)(void *, godot_rid *, const godot_vector2i *, int32_t, const godot_vector2 *);
godot_rect2 (*font_get_glyph_uv_rect)(const void *, godot_rid *, const godot_vector2i *, int32_t);
void (*font_set_glyph_uv_rect)(void *, godot_rid *, const godot_vector2i *, int32_t, const godot_rect2 *);
godot_int (*font_get_glyph_texture_idx)(const void *, godot_rid *, const godot_vector2i *, int32_t);
void (*font_set_glyph_texture_idx)(void *, godot_rid *, const godot_vector2i *, int32_t, godot_int);
bool (*font_get_glyph_contours)(const void *, godot_rid *, godot_int, int32_t, godot_packed_vector3_array *, godot_packed_int32_array *, bool *);
godot_array (*font_get_kerning_list)(const void *, godot_rid *, godot_int);
void (*font_clear_kerning_map)(void *, godot_rid *, godot_int);
void (*font_remove_kerning)(void *, godot_rid *, godot_int, const godot_vector2i *);
void (*font_set_kerning)(void *, godot_rid *, godot_int, const godot_vector2i *, const godot_vector2 *);
godot_vector2 (*font_get_kerning)(const void *, godot_rid *, godot_int, const godot_vector2i *);
int32_t (*font_get_glyph_index)(const void *, godot_rid *, godot_int, char32_t, char32_t);
bool (*font_has_char)(const void *, godot_rid *, char32_t);
godot_string (*font_get_supported_chars)(const void *, godot_rid *);
void (*font_render_range)(void *, godot_rid *, const godot_vector2i *, char32_t, char32_t);
void (*font_render_glyph)(void *, godot_rid *, const godot_vector2i *, int32_t);
void (*font_draw_glyph)(const void *, godot_rid *, godot_rid *, godot_int, const godot_vector2 *, int32_t, const godot_color *);
void (*font_draw_glyph_outline)(const void *, godot_rid *, godot_rid *, godot_int, godot_int, const godot_vector2 *, int32_t, const godot_color *);
bool (*font_is_language_supported)(const void *, godot_rid *, const godot_string *);
void (*font_set_language_support_override)(void *, godot_rid *, const godot_string *, bool);
bool (*font_get_language_support_override)(void *, godot_rid *, const godot_string *);
bool (*font_get_language_support_override)(const void *, godot_rid *, const godot_string *);
void (*font_remove_language_support_override)(void *, godot_rid *, const godot_string *);
godot_packed_string_array (*font_get_language_support_overrides)(void *, godot_rid *);
bool (*font_is_script_supported)(void *, godot_rid *, const godot_string *);
godot_packed_string_array (*font_get_language_support_overrides)(const void *, godot_rid *);
bool (*font_is_script_supported)(const void *, godot_rid *, const godot_string *);
void (*font_set_script_support_override)(void *, godot_rid *, const godot_string *, bool);
bool (*font_get_script_support_override)(void *, godot_rid *, const godot_string *);
bool (*font_get_script_support_override)(const void *, godot_rid *, const godot_string *);
void (*font_remove_script_support_override)(void *, godot_rid *, const godot_string *);
godot_packed_string_array (*font_get_script_support_overrides)(void *, godot_rid *);
uint32_t (*font_get_glyph_index)(void *, godot_rid *, char32_t, char32_t);
godot_vector2 (*font_get_glyph_advance)(void *, godot_rid *, uint32_t, int);
godot_vector2 (*font_get_glyph_kerning)(void *, godot_rid *, uint32_t, uint32_t, int);
godot_vector2 (*font_draw_glyph)(void *, godot_rid *, godot_rid *, int, const godot_vector2 *, uint32_t, const godot_color *);
godot_vector2 (*font_draw_glyph_outline)(void *, godot_rid *, godot_rid *, int, int, const godot_vector2 *, uint32_t, const godot_color *);
bool (*font_get_glyph_contours)(void *, godot_rid *, int, uint32_t, godot_packed_vector3_array *, godot_packed_int32_array *, bool *);
float (*font_get_oversampling)(void *);
void (*font_set_oversampling)(void *, float);
godot_packed_string_array (*get_system_fonts)(void *);
godot_packed_string_array (*font_get_script_support_overrides)(const void *, godot_rid *);
godot_dictionary (*font_supported_feature_list)(const void *, godot_rid *);
godot_dictionary (*font_supported_variation_list)(const void *, godot_rid *);
godot_real_t (*font_get_global_oversampling)(const void *);
void (*font_set_global_oversampling)(void *, godot_real_t);
godot_rid (*create_shaped_text)(void *, godot_int, godot_int);
void (*shaped_text_clear)(void *, godot_rid *);
void (*shaped_text_set_direction)(void *, godot_rid *, godot_int);
@ -138,27 +179,28 @@ typedef struct {
bool (*shaped_text_resize_object)(void *, godot_rid *, const godot_variant *, const godot_vector2 *, godot_int);
godot_rid (*shaped_text_substr)(void *, godot_rid *, godot_int, godot_int);
godot_rid (*shaped_text_get_parent)(void *, godot_rid *);
float (*shaped_text_fit_to_width)(void *, godot_rid *, float, uint8_t);
float (*shaped_text_tab_align)(void *, godot_rid *, godot_packed_float32_array *);
godot_real_t (*shaped_text_fit_to_width)(void *, godot_rid *, godot_real_t, uint8_t);
godot_real_t (*shaped_text_tab_align)(void *, godot_rid *, godot_packed_float32_array *);
bool (*shaped_text_shape)(void *, godot_rid *);
bool (*shaped_text_update_breaks)(void *, godot_rid *);
bool (*shaped_text_update_justification_ops)(void *, godot_rid *);
void (*shaped_text_overrun_trim_to_width)(void *, godot_rid *, float, uint8_t);
void (*shaped_text_overrun_trim_to_width)(void *, godot_rid *, godot_real_t, uint8_t);
bool (*shaped_text_is_ready)(void *, godot_rid *);
godot_packed_glyph_array (*shaped_text_get_glyphs)(void *, godot_rid *);
godot_vector2i (*shaped_text_get_range)(void *, godot_rid *);
godot_packed_glyph_array (*shaped_text_sort_logical)(void *, godot_rid *);
godot_packed_vector2i_array (*shaped_text_get_line_breaks_adv)(void *, godot_rid *, godot_packed_float32_array *, int, bool, uint8_t);
godot_packed_vector2i_array (*shaped_text_get_line_breaks)(void *, godot_rid *, float, int, uint8_t);
godot_packed_vector2i_array (*shaped_text_get_line_breaks)(void *, godot_rid *, godot_real_t, int, uint8_t);
godot_packed_vector2i_array (*shaped_text_get_word_breaks)(void *, godot_rid *, int);
godot_array (*shaped_text_get_objects)(void *, godot_rid *);
godot_rect2 (*shaped_text_get_object_rect)(void *, godot_rid *, const godot_variant *);
godot_vector2 (*shaped_text_get_size)(void *, godot_rid *);
float (*shaped_text_get_ascent)(void *, godot_rid *);
float (*shaped_text_get_descent)(void *, godot_rid *);
float (*shaped_text_get_width)(void *, godot_rid *);
float (*shaped_text_get_underline_position)(void *, godot_rid *);
float (*shaped_text_get_underline_thickness)(void *, godot_rid *);
godot_real_t (*shaped_text_get_ascent)(void *, godot_rid *);
godot_real_t (*shaped_text_get_descent)(void *, godot_rid *);
godot_real_t (*shaped_text_get_width)(void *, godot_rid *);
godot_real_t (*shaped_text_get_underline_position)(void *, godot_rid *);
godot_real_t (*shaped_text_get_underline_thickness)(void *, godot_rid *);
godot_string (*format_number)(void *, const godot_string *, const godot_string *);
godot_string (*parse_number)(void *, const godot_string *, const godot_string *);
godot_string (*percent_sign)(void *, const godot_string *);
@ -185,8 +227,8 @@ void GDAPI godot_glyph_set_flags(godot_glyph *p_self, godot_int p_flags);
godot_vector2 GDAPI godot_glyph_get_offset(const godot_glyph *p_self);
void GDAPI godot_glyph_set_offset(godot_glyph *p_self, const godot_vector2 *p_offset);
godot_float GDAPI godot_glyph_get_advance(const godot_glyph *p_self);
void GDAPI godot_glyph_set_advance(godot_glyph *p_self, godot_float p_advance);
godot_real_t GDAPI godot_glyph_get_advance(const godot_glyph *p_self);
void GDAPI godot_glyph_set_advance(godot_glyph *p_self, godot_real_t p_advance);
godot_rid GDAPI godot_glyph_get_font(const godot_glyph *p_self);
void GDAPI godot_glyph_set_font(godot_glyph *p_self, godot_rid *p_font);

View File

@ -88,299 +88,479 @@ bool TextServerGDNative::is_locale_right_to_left(const String &p_locale) {
return interface->is_locale_right_to_left(data, (godot_string *)&p_locale);
}
/*************************************************************************/
/* Font interface */
/*************************************************************************/
RID TextServerGDNative::create_font_system(const String &p_name, int p_base_size) {
ERR_FAIL_COND_V(interface == nullptr, RID());
godot_rid result = interface->create_font_system(data, (const godot_string *)&p_name, p_base_size);
RID rid = *(RID *)&result;
return rid;
}
RID TextServerGDNative::create_font_resource(const String &p_filename, int p_base_size) {
ERR_FAIL_COND_V(interface == nullptr, RID());
godot_rid result = interface->create_font_resource(data, (const godot_string *)&p_filename, p_base_size);
RID rid = *(RID *)&result;
return rid;
}
RID TextServerGDNative::create_font_memory(const uint8_t *p_data, size_t p_size, const String &p_type, int p_base_size) {
ERR_FAIL_COND_V(interface == nullptr, RID());
godot_rid result = interface->create_font_memory(data, p_data, p_size, (godot_string *)&p_type, p_base_size);
RID rid = *(RID *)&result;
return rid;
}
RID TextServerGDNative::create_font_bitmap(float p_height, float p_ascent, int p_base_size) {
ERR_FAIL_COND_V(interface == nullptr, RID());
godot_rid result = interface->create_font_bitmap(data, p_height, p_ascent, p_base_size);
RID rid = *(RID *)&result;
return rid;
}
void TextServerGDNative::font_bitmap_add_texture(RID p_font, const Ref<Texture> &p_texture) {
ERR_FAIL_COND(interface == nullptr);
interface->font_bitmap_add_texture(data, (godot_rid *)&p_font, (const godot_object *)p_texture.ptr());
}
void TextServerGDNative::font_bitmap_add_char(RID p_font, char32_t p_char, int p_texture_idx, const Rect2 &p_rect, const Size2 &p_align, float p_advance) {
ERR_FAIL_COND(interface == nullptr);
interface->font_bitmap_add_char(data, (godot_rid *)&p_font, p_char, p_texture_idx, (const godot_rect2 *)&p_rect, (const godot_vector2 *)&p_align, p_advance);
}
void TextServerGDNative::font_bitmap_add_kerning_pair(RID p_font, char32_t p_A, char32_t p_B, int p_kerning) {
ERR_FAIL_COND(interface == nullptr);
interface->font_bitmap_add_kerning_pair(data, (godot_rid *)&p_font, p_A, p_B, p_kerning);
}
float TextServerGDNative::font_get_height(RID p_font, int p_size) const {
ERR_FAIL_COND_V(interface == nullptr, 0.f);
return interface->font_get_height(data, (godot_rid *)&p_font, p_size);
}
float TextServerGDNative::font_get_ascent(RID p_font, int p_size) const {
ERR_FAIL_COND_V(interface == nullptr, 0.f);
return interface->font_get_ascent(data, (godot_rid *)&p_font, p_size);
}
float TextServerGDNative::font_get_descent(RID p_font, int p_size) const {
ERR_FAIL_COND_V(interface == nullptr, 0.f);
return interface->font_get_descent(data, (godot_rid *)&p_font, p_size);
}
float TextServerGDNative::font_get_underline_position(RID p_font, int p_size) const {
ERR_FAIL_COND_V(interface == nullptr, 0.f);
return interface->font_get_underline_position(data, (godot_rid *)&p_font, p_size);
}
float TextServerGDNative::font_get_underline_thickness(RID p_font, int p_size) const {
ERR_FAIL_COND_V(interface == nullptr, 0.f);
return interface->font_get_underline_thickness(data, (godot_rid *)&p_font, p_size);
}
int TextServerGDNative::font_get_spacing_space(RID p_font) const {
int32_t TextServerGDNative::name_to_tag(const String &p_name) const {
ERR_FAIL_COND_V(interface == nullptr, 0);
return interface->font_get_spacing_space(data, (godot_rid *)&p_font);
return interface->name_to_tag(data, (godot_string *)&p_name);
}
void TextServerGDNative::font_set_spacing_space(RID p_font, int p_value) {
ERR_FAIL_COND(interface == nullptr);
interface->font_set_spacing_space(data, (godot_rid *)&p_font, p_value);
}
int TextServerGDNative::font_get_spacing_glyph(RID p_font) const {
ERR_FAIL_COND_V(interface == nullptr, 0);
return interface->font_get_spacing_glyph(data, (godot_rid *)&p_font);
}
void TextServerGDNative::font_set_spacing_glyph(RID p_font, int p_value) {
ERR_FAIL_COND(interface == nullptr);
interface->font_set_spacing_glyph(data, (godot_rid *)&p_font, p_value);
}
void TextServerGDNative::font_set_antialiased(RID p_font, bool p_antialiased) {
ERR_FAIL_COND(interface == nullptr);
interface->font_set_antialiased(data, (godot_rid *)&p_font, p_antialiased);
}
bool TextServerGDNative::font_get_antialiased(RID p_font) const {
ERR_FAIL_COND_V(interface == nullptr, false);
return interface->font_get_antialiased(data, (godot_rid *)&p_font);
}
Dictionary TextServerGDNative::font_get_variation_list(RID p_font) const {
ERR_FAIL_COND_V(interface == nullptr, Dictionary());
godot_dictionary result = interface->font_get_variation_list(data, (godot_rid *)&p_font);
Dictionary info = *(Dictionary *)&result;
godot_dictionary_destroy(&result);
return info;
}
void TextServerGDNative::font_set_variation(RID p_font, const String &p_name, double p_value) {
ERR_FAIL_COND(interface == nullptr);
interface->font_set_variation(data, (godot_rid *)&p_font, (godot_string *)&p_name, p_value);
}
double TextServerGDNative::font_get_variation(RID p_font, const String &p_name) const {
return interface->font_get_variation(data, (godot_rid *)&p_font, (godot_string *)&p_name);
}
void TextServerGDNative::font_set_hinting(RID p_font, TextServer::Hinting p_hinting) {
ERR_FAIL_COND(interface == nullptr);
interface->font_set_hinting(data, (godot_rid *)&p_font, (godot_int)p_hinting);
}
TextServer::Hinting TextServerGDNative::font_get_hinting(RID p_font) const {
ERR_FAIL_COND_V(interface == nullptr, TextServer::HINTING_NONE);
return (TextServer::Hinting)interface->font_get_hinting(data, (godot_rid *)&p_font);
}
Dictionary TextServerGDNative::font_get_feature_list(RID p_font) const {
ERR_FAIL_COND_V(interface == nullptr, Dictionary());
godot_dictionary result = interface->font_get_feature_list(data, (godot_rid *)&p_font);
Dictionary info = *(Dictionary *)&result;
godot_dictionary_destroy(&result);
return info;
}
void TextServerGDNative::font_set_distance_field_hint(RID p_font, bool p_distance_field) {
ERR_FAIL_COND(interface == nullptr);
interface->font_set_distance_field_hint(data, (godot_rid *)&p_font, p_distance_field);
}
bool TextServerGDNative::font_get_distance_field_hint(RID p_font) const {
ERR_FAIL_COND_V(interface == nullptr, false);
return interface->font_get_distance_field_hint(data, (godot_rid *)&p_font);
}
void TextServerGDNative::font_set_force_autohinter(RID p_font, bool p_enabeld) {
ERR_FAIL_COND(interface == nullptr);
interface->font_set_force_autohinter(data, (godot_rid *)&p_font, p_enabeld);
}
bool TextServerGDNative::font_get_force_autohinter(RID p_font) const {
ERR_FAIL_COND_V(interface == nullptr, false);
return interface->font_get_force_autohinter(data, (godot_rid *)&p_font);
}
bool TextServerGDNative::font_has_char(RID p_font, char32_t p_char) const {
ERR_FAIL_COND_V(interface == nullptr, false);
return interface->font_has_char(data, (godot_rid *)&p_font, p_char);
}
String TextServerGDNative::font_get_supported_chars(RID p_font) const {
String TextServerGDNative::tag_to_name(int32_t p_tag) const {
ERR_FAIL_COND_V(interface == nullptr, String());
godot_string result = interface->font_get_supported_chars(data, (godot_rid *)&p_font);
String ret = *(String *)&result;
godot_string result = interface->tag_to_name(data, p_tag);
String name = *(String *)&result;
godot_string_destroy(&result);
return ret;
return name;
}
bool TextServerGDNative::font_has_outline(RID p_font) const {
ERR_FAIL_COND_V(interface == nullptr, false);
return interface->font_has_outline(data, (godot_rid *)&p_font);
/*************************************************************************/
/* Font */
/*************************************************************************/
RID TextServerGDNative::create_font() {
ERR_FAIL_COND_V(interface == nullptr, RID());
godot_rid result = interface->create_font(data);
RID rid = *(RID *)&result;
return rid;
}
float TextServerGDNative::font_get_base_size(RID p_font) const {
ERR_FAIL_COND_V(interface == nullptr, 0.f);
return interface->font_get_base_size(data, (godot_rid *)&p_font);
}
bool TextServerGDNative::font_is_language_supported(RID p_font, const String &p_language) const {
ERR_FAIL_COND_V(interface == nullptr, false);
return interface->font_is_language_supported(data, (godot_rid *)&p_font, (godot_string *)&p_language);
}
void TextServerGDNative::font_set_language_support_override(RID p_font, const String &p_language, bool p_supported) {
void TextServerGDNative::font_set_data(RID p_font_rid, const PackedByteArray &p_data) {
ERR_FAIL_COND(interface == nullptr);
return interface->font_set_language_support_override(data, (godot_rid *)&p_font, (godot_string *)&p_language, p_supported);
interface->font_set_data(data, (godot_rid *)&p_font_rid, (const godot_packed_byte_array *)&p_data);
}
bool TextServerGDNative::font_get_language_support_override(RID p_font, const String &p_language) {
ERR_FAIL_COND_V(interface == nullptr, false);
return interface->font_get_language_support_override(data, (godot_rid *)&p_font, (godot_string *)&p_language);
}
void TextServerGDNative::font_remove_language_support_override(RID p_font, const String &p_language) {
void TextServerGDNative::font_set_data_ptr(RID p_font_rid, const uint8_t *p_data_ptr, size_t p_data_size) {
ERR_FAIL_COND(interface == nullptr);
interface->font_remove_language_support_override(data, (godot_rid *)&p_font, (godot_string *)&p_language);
interface->font_set_data_ptr(data, (godot_rid *)&p_font_rid, p_data_ptr, p_data_size);
}
Vector<String> TextServerGDNative::font_get_language_support_overrides(RID p_font) {
ERR_FAIL_COND_V(interface == nullptr, Vector<String>());
godot_packed_string_array result = interface->font_get_language_support_overrides(data, (godot_rid *)&p_font);
Vector<String> ret = *(Vector<String> *)&result;
godot_packed_string_array_destroy(&result);
return ret;
}
bool TextServerGDNative::font_is_script_supported(RID p_font, const String &p_script) const {
ERR_FAIL_COND_V(interface == nullptr, false);
return interface->font_is_script_supported(data, (godot_rid *)&p_font, (godot_string *)&p_script);
}
void TextServerGDNative::font_set_script_support_override(RID p_font, const String &p_script, bool p_supported) {
void TextServerGDNative::font_set_antialiased(RID p_font_rid, bool p_antialiased) {
ERR_FAIL_COND(interface == nullptr);
return interface->font_set_script_support_override(data, (godot_rid *)&p_font, (godot_string *)&p_script, p_supported);
interface->font_set_antialiased(data, (godot_rid *)&p_font_rid, p_antialiased);
}
bool TextServerGDNative::font_get_script_support_override(RID p_font, const String &p_script) {
bool TextServerGDNative::font_is_antialiased(RID p_font_rid) const {
ERR_FAIL_COND_V(interface == nullptr, false);
return interface->font_get_script_support_override(data, (godot_rid *)&p_font, (godot_string *)&p_script);
return interface->font_is_antialiased(data, (godot_rid *)&p_font_rid);
}
void TextServerGDNative::font_remove_script_support_override(RID p_font, const String &p_script) {
void TextServerGDNative::font_set_multichannel_signed_distance_field(RID p_font_rid, bool p_msdf) {
ERR_FAIL_COND(interface == nullptr);
interface->font_remove_script_support_override(data, (godot_rid *)&p_font, (godot_string *)&p_script);
interface->font_set_multichannel_signed_distance_field(data, (godot_rid *)&p_font_rid, p_msdf);
}
Vector<String> TextServerGDNative::font_get_script_support_overrides(RID p_font) {
ERR_FAIL_COND_V(interface == nullptr, Vector<String>());
godot_packed_string_array result = interface->font_get_script_support_overrides(data, (godot_rid *)&p_font);
Vector<String> ret = *(Vector<String> *)&result;
godot_packed_string_array_destroy(&result);
return ret;
bool TextServerGDNative::font_is_multichannel_signed_distance_field(RID p_font_rid) const {
ERR_FAIL_COND_V(interface == nullptr, false);
return interface->font_is_multichannel_signed_distance_field(data, (godot_rid *)&p_font_rid);
}
uint32_t TextServerGDNative::font_get_glyph_index(RID p_font, char32_t p_char, char32_t p_variation_selector) const {
void TextServerGDNative::font_set_msdf_pixel_range(RID p_font_rid, int p_msdf_pixel_range) {
ERR_FAIL_COND(interface == nullptr);
interface->font_set_msdf_pixel_range(data, (godot_rid *)&p_font_rid, p_msdf_pixel_range);
}
int TextServerGDNative::font_get_msdf_pixel_range(RID p_font_rid) const {
ERR_FAIL_COND_V(interface == nullptr, 0);
return interface->font_get_glyph_index(data, (godot_rid *)&p_font, p_char, p_variation_selector);
return interface->font_get_msdf_pixel_range(data, (godot_rid *)&p_font_rid);
}
Vector2 TextServerGDNative::font_get_glyph_advance(RID p_font, uint32_t p_index, int p_size) const {
ERR_FAIL_COND_V(interface == nullptr, Vector2());
godot_vector2 result = interface->font_get_glyph_advance(data, (godot_rid *)&p_font, p_index, p_size);
Vector2 advance = *(Vector2 *)&result;
return advance;
}
Vector2 TextServerGDNative::font_get_glyph_kerning(RID p_font, uint32_t p_index_a, uint32_t p_index_b, int p_size) const {
ERR_FAIL_COND_V(interface == nullptr, Vector2());
godot_vector2 result = interface->font_get_glyph_kerning(data, (godot_rid *)&p_font, p_index_a, p_index_b, p_size);
Vector2 kerning = *(Vector2 *)&result;
return kerning;
}
Vector2 TextServerGDNative::font_draw_glyph(RID p_font, RID p_canvas, int p_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color) const {
ERR_FAIL_COND_V(interface == nullptr, Vector2());
godot_vector2 result = interface->font_draw_glyph(data, (godot_rid *)&p_font, (godot_rid *)&p_canvas, p_size, (const godot_vector2 *)&p_pos, p_index, (const godot_color *)&p_color);
Vector2 advance = *(Vector2 *)&result;
return advance;
}
Vector2 TextServerGDNative::font_draw_glyph_outline(RID p_font, RID p_canvas, int p_size, int p_outline_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color) const {
ERR_FAIL_COND_V(interface == nullptr, Vector2());
godot_vector2 result = interface->font_draw_glyph_outline(data, (godot_rid *)&p_font, (godot_rid *)&p_canvas, p_size, p_outline_size, (const godot_vector2 *)&p_pos, p_index, (const godot_color *)&p_color);
Vector2 advance = *(Vector2 *)&result;
return advance;
}
bool TextServerGDNative::font_get_glyph_contours(RID p_font, int p_size, uint32_t p_index, Vector<Vector3> &r_points, Vector<int32_t> &r_contours, bool &r_orientation) const {
ERR_FAIL_COND_V(interface == nullptr, false);
ERR_FAIL_COND_V(interface->font_get_glyph_contours == nullptr, false);
return interface->font_get_glyph_contours(data, (godot_rid *)&p_font, p_size, p_index, (godot_packed_vector3_array *)&r_points, (godot_packed_int32_array *)&r_contours, (bool *)&r_orientation);
}
float TextServerGDNative::font_get_oversampling() const {
ERR_FAIL_COND_V(interface == nullptr, 1.f);
return interface->font_get_oversampling(data);
}
void TextServerGDNative::font_set_oversampling(float p_oversampling) {
void TextServerGDNative::font_set_msdf_size(RID p_font_rid, int p_msdf_size) {
ERR_FAIL_COND(interface == nullptr);
return interface->font_set_oversampling(data, p_oversampling);
interface->font_set_msdf_size(data, (godot_rid *)&p_font_rid, p_msdf_size);
}
Vector<String> TextServerGDNative::get_system_fonts() const {
ERR_FAIL_COND_V(interface == nullptr, Vector<String>());
godot_packed_string_array result = interface->get_system_fonts(data);
Vector<String> fonts = *(Vector<String> *)&result;
int TextServerGDNative::font_get_msdf_size(RID p_font_rid) const {
ERR_FAIL_COND_V(interface == nullptr, 0);
return interface->font_get_msdf_size(data, (godot_rid *)&p_font_rid);
}
void TextServerGDNative::font_set_fixed_size(RID p_font_rid, int p_fixed_size) {
ERR_FAIL_COND(interface == nullptr);
interface->font_set_fixed_size(data, (godot_rid *)&p_font_rid, p_fixed_size);
}
int TextServerGDNative::font_get_fixed_size(RID p_font_rid) const {
ERR_FAIL_COND_V(interface == nullptr, 0);
return interface->font_get_fixed_size(data, (godot_rid *)&p_font_rid);
}
void TextServerGDNative::font_set_force_autohinter(RID p_font_rid, bool p_force_autohinter) {
ERR_FAIL_COND(interface == nullptr);
interface->font_set_force_autohinter(data, (godot_rid *)&p_font_rid, p_force_autohinter);
}
bool TextServerGDNative::font_is_force_autohinter(RID p_font_rid) const {
ERR_FAIL_COND_V(interface == nullptr, false);
return interface->font_is_force_autohinter(data, (godot_rid *)&p_font_rid);
}
void TextServerGDNative::font_set_hinting(RID p_font_rid, TextServer::Hinting p_hinting) {
ERR_FAIL_COND(interface == nullptr);
interface->font_set_hinting(data, (godot_rid *)&p_font_rid, (godot_int)p_hinting);
}
TextServer::Hinting TextServerGDNative::font_get_hinting(RID p_font_rid) const {
ERR_FAIL_COND_V(interface == nullptr, TextServer::HINTING_NONE);
return (TextServer::Hinting)interface->font_get_hinting(data, (godot_rid *)&p_font_rid);
}
void TextServerGDNative::font_set_variation_coordinates(RID p_font_rid, const Dictionary &p_variation_coordinates) {
ERR_FAIL_COND(interface == nullptr);
interface->font_set_variation_coordinates(data, (godot_rid *)&p_font_rid, (const godot_dictionary *)&p_variation_coordinates);
}
Dictionary TextServerGDNative::font_get_variation_coordinates(RID p_font_rid) const {
ERR_FAIL_COND_V(interface == nullptr, Dictionary());
godot_dictionary result = interface->font_get_variation_coordinates(data, (godot_rid *)&p_font_rid);
Dictionary dict = *(Dictionary *)&result;
godot_dictionary_destroy(&result);
return dict;
}
void TextServerGDNative::font_set_oversampling(RID p_font_rid, real_t p_oversampling) {
ERR_FAIL_COND(interface == nullptr);
interface->font_set_oversampling(data, (godot_rid *)&p_font_rid, p_oversampling);
}
real_t TextServerGDNative::font_get_oversampling(RID p_font_rid) const {
ERR_FAIL_COND_V(interface == nullptr, 0.0f);
return interface->font_get_oversampling(data, (godot_rid *)&p_font_rid);
}
Array TextServerGDNative::font_get_size_cache_list(RID p_font_rid) const {
ERR_FAIL_COND_V(interface == nullptr, Array());
godot_array result = interface->font_get_size_cache_list(data, (godot_rid *)&p_font_rid);
Array list = *(Array *)&result;
godot_array_destroy(&result);
return list;
}
void TextServerGDNative::font_clear_size_cache(RID p_font_rid) {
ERR_FAIL_COND(interface == nullptr);
interface->font_clear_size_cache(data, (godot_rid *)&p_font_rid);
}
void TextServerGDNative::font_remove_size_cache(RID p_font_rid, const Vector2i &p_size) {
ERR_FAIL_COND(interface == nullptr);
interface->font_remove_size_cache(data, (godot_rid *)&p_font_rid, (const godot_vector2i *)&p_size);
}
void TextServerGDNative::font_set_ascent(RID p_font_rid, int p_size, real_t p_ascent) {
ERR_FAIL_COND(interface == nullptr);
interface->font_set_ascent(data, (godot_rid *)&p_font_rid, p_size, p_ascent);
}
real_t TextServerGDNative::font_get_ascent(RID p_font_rid, int p_size) const {
ERR_FAIL_COND_V(interface == nullptr, 0.0f);
return interface->font_get_ascent(data, (godot_rid *)&p_font_rid, p_size);
}
void TextServerGDNative::font_set_descent(RID p_font_rid, int p_size, real_t p_descent) {
ERR_FAIL_COND(interface == nullptr);
interface->font_set_descent(data, (godot_rid *)&p_font_rid, p_size, p_descent);
}
real_t TextServerGDNative::font_get_descent(RID p_font_rid, int p_size) const {
ERR_FAIL_COND_V(interface == nullptr, 0.0f);
return interface->font_get_descent(data, (godot_rid *)&p_font_rid, p_size);
}
void TextServerGDNative::font_set_underline_position(RID p_font_rid, int p_size, real_t p_underline_position) {
ERR_FAIL_COND(interface == nullptr);
interface->font_set_underline_position(data, (godot_rid *)&p_font_rid, p_size, p_underline_position);
}
real_t TextServerGDNative::font_get_underline_position(RID p_font_rid, int p_size) const {
ERR_FAIL_COND_V(interface == nullptr, 0.0f);
return interface->font_get_underline_position(data, (godot_rid *)&p_font_rid, p_size);
}
void TextServerGDNative::font_set_underline_thickness(RID p_font_rid, int p_size, real_t p_underline_thickness) {
ERR_FAIL_COND(interface == nullptr);
interface->font_set_underline_thickness(data, (godot_rid *)&p_font_rid, p_size, p_underline_thickness);
}
real_t TextServerGDNative::font_get_underline_thickness(RID p_font_rid, int p_size) const {
ERR_FAIL_COND_V(interface == nullptr, 0.0f);
return interface->font_get_underline_thickness(data, (godot_rid *)&p_font_rid, p_size);
}
void TextServerGDNative::font_set_scale(RID p_font_rid, int p_size, real_t p_scale) {
ERR_FAIL_COND(interface == nullptr);
interface->font_set_scale(data, (godot_rid *)&p_font_rid, p_size, p_scale);
}
real_t TextServerGDNative::font_get_scale(RID p_font_rid, int p_size) const {
ERR_FAIL_COND_V(interface == nullptr, 0.0f);
return interface->font_get_scale(data, (godot_rid *)&p_font_rid, p_size);
}
void TextServerGDNative::font_set_spacing(RID p_font_rid, int p_size, TextServer::SpacingType p_spacing, int p_value) {
ERR_FAIL_COND(interface == nullptr);
interface->font_set_spacing(data, (godot_rid *)&p_font_rid, p_size, (godot_int)p_spacing, p_value);
}
int TextServerGDNative::font_get_spacing(RID p_font_rid, int p_size, TextServer::SpacingType p_spacing) const {
ERR_FAIL_COND_V(interface == nullptr, 0);
return interface->font_get_spacing(data, (godot_rid *)&p_font_rid, p_size, (godot_int)p_spacing);
}
int TextServerGDNative::font_get_texture_count(RID p_font_rid, const Vector2i &p_size) const {
ERR_FAIL_COND_V(interface == nullptr, -1);
return interface->font_get_texture_count(data, (godot_rid *)&p_font_rid, (const godot_vector2i *)&p_size);
}
void TextServerGDNative::font_clear_textures(RID p_font_rid, const Vector2i &p_size) {
ERR_FAIL_COND(interface == nullptr);
interface->font_clear_textures(data, (godot_rid *)&p_font_rid, (const godot_vector2i *)&p_size);
}
void TextServerGDNative::font_remove_texture(RID p_font_rid, const Vector2i &p_size, int p_texture_index) {
ERR_FAIL_COND(interface == nullptr);
interface->font_remove_texture(data, (godot_rid *)&p_font_rid, (const godot_vector2i *)&p_size, p_texture_index);
}
void TextServerGDNative::font_set_texture_image(RID p_font_rid, const Vector2i &p_size, int p_texture_index, const Ref<Image> &p_image) {
ERR_FAIL_COND(interface == nullptr);
interface->font_set_texture_image(data, (godot_rid *)&p_font_rid, (const godot_vector2i *)&p_size, p_texture_index, (const godot_object *)p_image.ptr());
}
Ref<Image> TextServerGDNative::font_get_texture_image(RID p_font_rid, const Vector2i &p_size, int p_texture_index) const {
ERR_FAIL_COND_V(interface == nullptr, Ref<Image>());
godot_object *result = interface->font_get_texture_image(data, (godot_rid *)&p_font_rid, (const godot_vector2i *)&p_size, p_texture_index);
return Ref<Image>((Image *)result);
}
void TextServerGDNative::font_set_texture_offsets(RID p_font_rid, const Vector2i &p_size, int p_texture_index, const PackedInt32Array &p_offset) {
ERR_FAIL_COND(interface == nullptr);
interface->font_set_texture_offsets(data, (godot_rid *)&p_font_rid, (const godot_vector2i *)&p_size, p_texture_index, (const godot_packed_int32_array *)&p_offset);
}
PackedInt32Array TextServerGDNative::font_get_texture_offsets(RID p_font_rid, const Vector2i &p_size, int p_texture_index) const {
ERR_FAIL_COND_V(interface == nullptr, PackedInt32Array());
godot_packed_int32_array result = interface->font_get_texture_offsets(data, (godot_rid *)&p_font_rid, (const godot_vector2i *)&p_size, p_texture_index);
PackedInt32Array offset = *(PackedInt32Array *)&result;
godot_packed_int32_array_destroy(&result);
return offset;
}
Array TextServerGDNative::font_get_glyph_list(RID p_font_rid, const Vector2i &p_size) const {
ERR_FAIL_COND_V(interface == nullptr, Array());
godot_array result = interface->font_get_glyph_list(data, (godot_rid *)&p_font_rid, (const godot_vector2i *)&p_size);
Array list = *(Array *)&result;
godot_array_destroy(&result);
return list;
}
void TextServerGDNative::font_clear_glyphs(RID p_font_rid, const Vector2i &p_size) {
ERR_FAIL_COND(interface == nullptr);
interface->font_clear_glyphs(data, (godot_rid *)&p_font_rid, (const godot_vector2i *)&p_size);
}
void TextServerGDNative::font_remove_glyph(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph) {
ERR_FAIL_COND(interface == nullptr);
interface->font_remove_glyph(data, (godot_rid *)&p_font_rid, (const godot_vector2i *)&p_size, p_glyph);
}
Vector2 TextServerGDNative::font_get_glyph_advance(RID p_font_rid, int p_size, int32_t p_glyph) const {
ERR_FAIL_COND_V(interface == nullptr, Vector2());
godot_vector2 result = interface->font_get_glyph_advance(data, (godot_rid *)&p_font_rid, p_size, p_glyph);
Vector2 adv = *(Vector2 *)&result;
return adv;
}
void TextServerGDNative::font_set_glyph_advance(RID p_font_rid, int p_size, int32_t p_glyph, const Vector2 &p_advance) {
ERR_FAIL_COND(interface == nullptr);
interface->font_set_glyph_advance(data, (godot_rid *)&p_font_rid, p_size, p_glyph, (const godot_vector2 *)&p_advance);
}
Vector2 TextServerGDNative::font_get_glyph_offset(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph) const {
ERR_FAIL_COND_V(interface == nullptr, Vector2());
godot_vector2 result = interface->font_get_glyph_offset(data, (godot_rid *)&p_font_rid, (const godot_vector2i *)&p_size, p_glyph);
Vector2 off = *(Vector2 *)&result;
return off;
}
void TextServerGDNative::font_set_glyph_offset(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph, const Vector2 &p_offset) {
ERR_FAIL_COND(interface == nullptr);
interface->font_set_glyph_offset(data, (godot_rid *)&p_font_rid, (const godot_vector2i *)&p_size, p_glyph, (const godot_vector2 *)&p_offset);
}
Vector2 TextServerGDNative::font_get_glyph_size(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph) const {
ERR_FAIL_COND_V(interface == nullptr, Vector2());
godot_vector2 result = interface->font_get_glyph_size(data, (godot_rid *)&p_font_rid, (const godot_vector2i *)&p_size, p_glyph);
Vector2 sz = *(Vector2 *)&result;
return sz;
}
void TextServerGDNative::font_set_glyph_size(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph, const Vector2 &p_gl_size) {
ERR_FAIL_COND(interface == nullptr);
interface->font_set_glyph_size(data, (godot_rid *)&p_font_rid, (const godot_vector2i *)&p_size, p_glyph, (const godot_vector2 *)&p_size);
}
Rect2 TextServerGDNative::font_get_glyph_uv_rect(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph) const {
ERR_FAIL_COND_V(interface == nullptr, Rect2());
godot_rect2 result = interface->font_get_glyph_uv_rect(data, (godot_rid *)&p_font_rid, (const godot_vector2i *)&p_size, p_glyph);
Rect2 uv = *(Rect2 *)&result;
return uv;
}
void TextServerGDNative::font_set_glyph_uv_rect(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph, const Rect2 &p_uv_rect) {
ERR_FAIL_COND(interface == nullptr);
interface->font_set_glyph_uv_rect(data, (godot_rid *)&p_font_rid, (const godot_vector2i *)&p_size, p_glyph, (const godot_rect2 *)&p_uv_rect);
}
int TextServerGDNative::font_get_glyph_texture_idx(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph) const {
ERR_FAIL_COND_V(interface == nullptr, -1);
return interface->font_get_glyph_texture_idx(data, (godot_rid *)&p_font_rid, (const godot_vector2i *)&p_size, p_glyph);
}
void TextServerGDNative::font_set_glyph_texture_idx(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph, int p_texture_idx) {
ERR_FAIL_COND(interface == nullptr);
interface->font_set_glyph_texture_idx(data, (godot_rid *)&p_font_rid, (const godot_vector2i *)&p_size, p_glyph, p_texture_idx);
}
bool TextServerGDNative::font_get_glyph_contours(RID p_font_rid, int p_size, int32_t p_index, Vector<Vector3> &r_points, Vector<int32_t> &r_contours, bool &r_orientation) const {
ERR_FAIL_COND_V(interface == nullptr, false);
return interface->font_get_glyph_contours(data, (godot_rid *)&p_font_rid, p_size, p_index, (godot_packed_vector3_array *)&r_points, (godot_packed_int32_array *)&r_contours, &r_orientation);
}
Array TextServerGDNative::font_get_kerning_list(RID p_font_rid, int p_size) const {
ERR_FAIL_COND_V(interface == nullptr, Array());
godot_array result = interface->font_get_kerning_list(data, (godot_rid *)&p_font_rid, p_size);
Array list = *(Array *)&result;
godot_array_destroy(&result);
return list;
}
void TextServerGDNative::font_clear_kerning_map(RID p_font_rid, int p_size) {
ERR_FAIL_COND(interface == nullptr);
interface->font_clear_kerning_map(data, (godot_rid *)&p_font_rid, p_size);
}
void TextServerGDNative::font_remove_kerning(RID p_font_rid, int p_size, const Vector2i &p_glyph_pair) {
ERR_FAIL_COND(interface == nullptr);
interface->font_remove_kerning(data, (godot_rid *)&p_font_rid, p_size, (const godot_vector2i *)&p_glyph_pair);
}
void TextServerGDNative::font_set_kerning(RID p_font_rid, int p_size, const Vector2i &p_glyph_pair, const Vector2 &p_kerning) {
ERR_FAIL_COND(interface == nullptr);
interface->font_set_kerning(data, (godot_rid *)&p_font_rid, p_size, (const godot_vector2i *)&p_glyph_pair, (const godot_vector2 *)&p_kerning);
}
Vector2 TextServerGDNative::font_get_kerning(RID p_font_rid, int p_size, const Vector2i &p_glyph_pair) const {
ERR_FAIL_COND_V(interface == nullptr, Vector2());
godot_vector2 result = interface->font_get_kerning(data, (godot_rid *)&p_font_rid, p_size, (const godot_vector2i *)&p_glyph_pair);
Vector2 kern = *(Vector2 *)&result;
return kern;
}
int32_t TextServerGDNative::font_get_glyph_index(RID p_font_rid, int p_size, char32_t p_char, char32_t p_variation_selector) const {
ERR_FAIL_COND_V(interface == nullptr, 0);
return interface->font_get_glyph_index(data, (godot_rid *)&p_font_rid, p_size, p_char, p_variation_selector);
}
bool TextServerGDNative::font_has_char(RID p_font_rid, char32_t p_char) const {
ERR_FAIL_COND_V(interface == nullptr, false);
return interface->font_has_char(data, (godot_rid *)&p_font_rid, p_char);
}
String TextServerGDNative::font_get_supported_chars(RID p_font_rid) const {
ERR_FAIL_COND_V(interface == nullptr, String());
godot_string result = interface->font_get_supported_chars(data, (godot_rid *)&p_font_rid);
String chars = *(String *)&result;
godot_string_destroy(&result);
return chars;
}
void TextServerGDNative::font_render_range(RID p_font_rid, const Vector2i &p_size, char32_t p_start, char32_t p_end) {
ERR_FAIL_COND(interface == nullptr);
interface->font_render_range(data, (godot_rid *)&p_font_rid, (const godot_vector2i *)&p_size, p_start, p_end);
}
void TextServerGDNative::font_render_glyph(RID p_font_rid, const Vector2i &p_size, int32_t p_index) {
ERR_FAIL_COND(interface == nullptr);
interface->font_render_glyph(data, (godot_rid *)&p_font_rid, (const godot_vector2i *)&p_size, p_index);
}
void TextServerGDNative::font_draw_glyph(RID p_font_rid, RID p_canvas, int p_size, const Vector2 &p_pos, int32_t p_index, const Color &p_color) const {
ERR_FAIL_COND(interface == nullptr);
interface->font_draw_glyph(data, (godot_rid *)&p_font_rid, (godot_rid *)&p_canvas, p_size, (const godot_vector2 *)&p_pos, p_index, (const godot_color *)&p_color);
}
void TextServerGDNative::font_draw_glyph_outline(RID p_font_rid, RID p_canvas, int p_size, int p_outline_size, const Vector2 &p_pos, int32_t p_index, const Color &p_color) const {
ERR_FAIL_COND(interface == nullptr);
interface->font_draw_glyph_outline(data, (godot_rid *)&p_font_rid, (godot_rid *)&p_canvas, p_size, p_outline_size, (const godot_vector2 *)&p_pos, p_index, (const godot_color *)&p_color);
}
bool TextServerGDNative::font_is_language_supported(RID p_font_rid, const String &p_language) const {
ERR_FAIL_COND_V(interface == nullptr, false);
return interface->font_is_language_supported(data, (godot_rid *)&p_font_rid, (const godot_string *)&p_language);
}
void TextServerGDNative::font_set_language_support_override(RID p_font_rid, const String &p_language, bool p_supported) {
ERR_FAIL_COND(interface == nullptr);
interface->font_set_language_support_override(data, (godot_rid *)&p_font_rid, (const godot_string *)&p_language, p_supported);
}
bool TextServerGDNative::font_get_language_support_override(RID p_font_rid, const String &p_language) {
ERR_FAIL_COND_V(interface == nullptr, false);
return interface->font_get_language_support_override(data, (godot_rid *)&p_font_rid, (const godot_string *)&p_language);
}
void TextServerGDNative::font_remove_language_support_override(RID p_font_rid, const String &p_language) {
ERR_FAIL_COND(interface == nullptr);
interface->font_remove_language_support_override(data, (godot_rid *)&p_font_rid, (const godot_string *)&p_language);
}
Vector<String> TextServerGDNative::font_get_language_support_overrides(RID p_font_rid) {
ERR_FAIL_COND_V(interface == nullptr, PackedStringArray());
godot_packed_string_array result = interface->font_get_language_support_overrides(data, (godot_rid *)&p_font_rid);
PackedStringArray list = *(PackedStringArray *)&result;
godot_packed_string_array_destroy(&result);
return fonts;
return list;
}
bool TextServerGDNative::font_is_script_supported(RID p_font_rid, const String &p_script) const {
ERR_FAIL_COND_V(interface == nullptr, false);
return interface->font_is_script_supported(data, (godot_rid *)&p_font_rid, (const godot_string *)&p_script);
}
void TextServerGDNative::font_set_script_support_override(RID p_font_rid, const String &p_script, bool p_supported) {
ERR_FAIL_COND(interface == nullptr);
interface->font_set_script_support_override(data, (godot_rid *)&p_font_rid, (const godot_string *)&p_script, p_supported);
}
bool TextServerGDNative::font_get_script_support_override(RID p_font_rid, const String &p_script) {
ERR_FAIL_COND_V(interface == nullptr, false);
return interface->font_get_script_support_override(data, (godot_rid *)&p_font_rid, (const godot_string *)&p_script);
}
void TextServerGDNative::font_remove_script_support_override(RID p_font_rid, const String &p_script) {
ERR_FAIL_COND(interface == nullptr);
interface->font_remove_script_support_override(data, (godot_rid *)&p_font_rid, (const godot_string *)&p_script);
}
Vector<String> TextServerGDNative::font_get_script_support_overrides(RID p_font_rid) {
ERR_FAIL_COND_V(interface == nullptr, PackedStringArray());
godot_packed_string_array result = interface->font_get_script_support_overrides(data, (godot_rid *)&p_font_rid);
PackedStringArray list = *(PackedStringArray *)&result;
godot_packed_string_array_destroy(&result);
return list;
}
Dictionary TextServerGDNative::font_supported_feature_list(RID p_font_rid) const {
ERR_FAIL_COND_V(interface == nullptr, Dictionary());
godot_dictionary result = interface->font_supported_feature_list(data, (godot_rid *)&p_font_rid);
Dictionary dict = *(Dictionary *)&result;
godot_dictionary_destroy(&result);
return dict;
}
Dictionary TextServerGDNative::font_supported_variation_list(RID p_font_rid) const {
ERR_FAIL_COND_V(interface == nullptr, Dictionary());
godot_dictionary result = interface->font_supported_variation_list(data, (godot_rid *)&p_font_rid);
Dictionary dict = *(Dictionary *)&result;
godot_dictionary_destroy(&result);
return dict;
}
real_t TextServerGDNative::font_get_global_oversampling() const {
ERR_FAIL_COND_V(interface == nullptr, 0.0f);
return interface->font_get_global_oversampling(data);
}
void TextServerGDNative::font_set_global_oversampling(real_t p_oversampling) {
ERR_FAIL_COND(interface == nullptr);
interface->font_set_global_oversampling(data, p_oversampling);
}
/*************************************************************************/
@ -473,12 +653,12 @@ RID TextServerGDNative::shaped_text_get_parent(RID p_shaped) const {
return rid;
}
float TextServerGDNative::shaped_text_fit_to_width(RID p_shaped, float p_width, uint8_t p_jst_flags) {
real_t TextServerGDNative::shaped_text_fit_to_width(RID p_shaped, real_t p_width, uint8_t p_jst_flags) {
ERR_FAIL_COND_V(interface == nullptr, 0.f);
return interface->shaped_text_fit_to_width(data, (godot_rid *)&p_shaped, p_width, p_jst_flags);
}
float TextServerGDNative::shaped_text_tab_align(RID p_shaped, const Vector<float> &p_tab_stops) {
real_t TextServerGDNative::shaped_text_tab_align(RID p_shaped, const Vector<real_t> &p_tab_stops) {
ERR_FAIL_COND_V(interface == nullptr, 0.f);
return interface->shaped_text_tab_align(data, (godot_rid *)&p_shaped, (godot_packed_float32_array *)&p_tab_stops);
}
@ -498,7 +678,7 @@ bool TextServerGDNative::shaped_text_update_justification_ops(RID p_shaped) {
return interface->shaped_text_update_justification_ops(data, (godot_rid *)&p_shaped);
}
void TextServerGDNative::shaped_text_overrun_trim_to_width(RID p_shaped_line, float p_width, uint8_t p_trim_flags) {
void TextServerGDNative::shaped_text_overrun_trim_to_width(RID p_shaped_line, real_t p_width, uint8_t p_trim_flags) {
ERR_FAIL_COND(interface == nullptr);
interface->shaped_text_overrun_trim_to_width(data, (godot_rid *)&p_shaped_line, p_width, p_trim_flags);
};
@ -531,7 +711,7 @@ Vector<TextServer::Glyph> TextServerGDNative::shaped_text_sort_logical(RID p_sha
return glyphs;
}
Vector<Vector2i> TextServerGDNative::shaped_text_get_line_breaks_adv(RID p_shaped, const Vector<float> &p_width, int p_start, bool p_once, uint8_t p_break_flags) const {
Vector<Vector2i> TextServerGDNative::shaped_text_get_line_breaks_adv(RID p_shaped, const Vector<real_t> &p_width, int p_start, bool p_once, uint8_t p_break_flags) const {
ERR_FAIL_COND_V(interface == nullptr, Vector<Vector2i>());
if (interface->shaped_text_get_line_breaks_adv != nullptr) {
godot_packed_vector2i_array result = interface->shaped_text_get_line_breaks_adv(data, (godot_rid *)&p_shaped, (godot_packed_float32_array *)&p_width, p_start, p_once, p_break_flags);
@ -543,7 +723,7 @@ Vector<Vector2i> TextServerGDNative::shaped_text_get_line_breaks_adv(RID p_shape
}
}
Vector<Vector2i> TextServerGDNative::shaped_text_get_line_breaks(RID p_shaped, float p_width, int p_start, uint8_t p_break_flags) const {
Vector<Vector2i> TextServerGDNative::shaped_text_get_line_breaks(RID p_shaped, real_t p_width, int p_start, uint8_t p_break_flags) const {
ERR_FAIL_COND_V(interface == nullptr, Vector<Vector2i>());
if (interface->shaped_text_get_line_breaks != nullptr) {
godot_packed_vector2i_array result = interface->shaped_text_get_line_breaks(data, (godot_rid *)&p_shaped, p_width, p_start, p_break_flags);
@ -588,27 +768,27 @@ Size2 TextServerGDNative::shaped_text_get_size(RID p_shaped) const {
return size;
}
float TextServerGDNative::shaped_text_get_ascent(RID p_shaped) const {
real_t TextServerGDNative::shaped_text_get_ascent(RID p_shaped) const {
ERR_FAIL_COND_V(interface == nullptr, 0.f);
return interface->shaped_text_get_ascent(data, (godot_rid *)&p_shaped);
}
float TextServerGDNative::shaped_text_get_descent(RID p_shaped) const {
real_t TextServerGDNative::shaped_text_get_descent(RID p_shaped) const {
ERR_FAIL_COND_V(interface == nullptr, 0.f);
return interface->shaped_text_get_descent(data, (godot_rid *)&p_shaped);
}
float TextServerGDNative::shaped_text_get_width(RID p_shaped) const {
real_t TextServerGDNative::shaped_text_get_width(RID p_shaped) const {
ERR_FAIL_COND_V(interface == nullptr, 0.f);
return interface->shaped_text_get_width(data, (godot_rid *)&p_shaped);
}
float TextServerGDNative::shaped_text_get_underline_position(RID p_shaped) const {
real_t TextServerGDNative::shaped_text_get_underline_position(RID p_shaped) const {
ERR_FAIL_COND_V(interface == nullptr, 0.f);
return interface->shaped_text_get_underline_position(data, (godot_rid *)&p_shaped);
}
float TextServerGDNative::shaped_text_get_underline_thickness(RID p_shaped) const {
real_t TextServerGDNative::shaped_text_get_underline_thickness(RID p_shaped) const {
ERR_FAIL_COND_V(interface == nullptr, 0.f);
return interface->shaped_text_get_underline_thickness(data, (godot_rid *)&p_shaped);
}
@ -756,12 +936,12 @@ void GDAPI godot_glyph_set_offset(godot_glyph *p_self, const godot_vector2 *p_of
self->y_off = offset->y;
}
godot_float GDAPI godot_glyph_get_advance(const godot_glyph *p_self) {
godot_real_t GDAPI godot_glyph_get_advance(const godot_glyph *p_self) {
const TextServer::Glyph *self = (const TextServer::Glyph *)p_self;
return self->advance;
}
void GDAPI godot_glyph_set_advance(godot_glyph *p_self, godot_float p_advance) {
void GDAPI godot_glyph_set_advance(godot_glyph *p_self, godot_real_t p_advance) {
TextServer::Glyph *self = (TextServer::Glyph *)p_self;
self->advance = p_advance;
}

View File

@ -60,78 +60,130 @@ public:
virtual bool is_locale_right_to_left(const String &p_locale) override;
virtual int32_t name_to_tag(const String &p_name) const override;
virtual String tag_to_name(int32_t p_tag) const override;
/* Font interface */
virtual RID create_font_system(const String &p_name, int p_base_size = 16) override;
virtual RID create_font_resource(const String &p_filename, int p_base_size = 16) override;
virtual RID create_font_memory(const uint8_t *p_data, size_t p_size, const String &p_type, int p_base_size = 16) override;
virtual RID create_font_bitmap(float p_height, float p_ascent, int p_base_size = 16) override;
virtual RID create_font() override;
virtual void font_bitmap_add_texture(RID p_font, const Ref<Texture> &p_texture) override;
virtual void font_bitmap_add_char(RID p_font, char32_t p_char, int p_texture_idx, const Rect2 &p_rect, const Size2 &p_align, float p_advance) override;
virtual void font_bitmap_add_kerning_pair(RID p_font, char32_t p_A, char32_t p_B, int p_kerning) override;
virtual void font_set_data(RID p_font_rid, const PackedByteArray &p_data) override;
virtual void font_set_data_ptr(RID p_font_rid, const uint8_t *p_data_ptr, size_t p_data_size) override;
virtual float font_get_height(RID p_font, int p_size) const override;
virtual float font_get_ascent(RID p_font, int p_size) const override;
virtual float font_get_descent(RID p_font, int p_size) const override;
virtual void font_set_antialiased(RID p_font_rid, bool p_antialiased) override;
virtual bool font_is_antialiased(RID p_font_rid) const override;
virtual float font_get_underline_position(RID p_font, int p_size) const override;
virtual float font_get_underline_thickness(RID p_font, int p_size) const override;
virtual void font_set_multichannel_signed_distance_field(RID p_font_rid, bool p_msdf) override;
virtual bool font_is_multichannel_signed_distance_field(RID p_font_rid) const override;
virtual int font_get_spacing_space(RID p_font) const override;
virtual void font_set_spacing_space(RID p_font, int p_value) override;
virtual void font_set_msdf_pixel_range(RID p_font_rid, int p_msdf_pixel_range) override;
virtual int font_get_msdf_pixel_range(RID p_font_rid) const override;
virtual int font_get_spacing_glyph(RID p_font) const override;
virtual void font_set_spacing_glyph(RID p_font, int p_value) override;
virtual void font_set_msdf_size(RID p_font_rid, int p_msdf_size) override;
virtual int font_get_msdf_size(RID p_font_rid) const override;
virtual void font_set_antialiased(RID p_font, bool p_antialiased) override;
virtual bool font_get_antialiased(RID p_font) const override;
virtual void font_set_fixed_size(RID p_font_rid, int p_fixed_size) override;
virtual int font_get_fixed_size(RID p_font_rid) const override;
virtual Dictionary font_get_feature_list(RID p_font) const override;
virtual Dictionary font_get_variation_list(RID p_font) const override;
virtual void font_set_force_autohinter(RID p_font_rid, bool p_force_autohinter) override;
virtual bool font_is_force_autohinter(RID p_font_rid) const override;
virtual void font_set_variation(RID p_font, const String &p_name, double p_value) override;
virtual double font_get_variation(RID p_font, const String &p_name) const override;
virtual void font_set_hinting(RID p_font_rid, TextServer::Hinting p_hinting) override;
virtual TextServer::Hinting font_get_hinting(RID p_font_rid) const override;
virtual void font_set_hinting(RID p_font, Hinting p_hinting) override;
virtual Hinting font_get_hinting(RID p_font) const override;
virtual void font_set_variation_coordinates(RID p_font_rid, const Dictionary &p_variation_coordinates) override;
virtual Dictionary font_get_variation_coordinates(RID p_font_rid) const override;
virtual void font_set_distance_field_hint(RID p_font, bool p_distance_field) override;
virtual bool font_get_distance_field_hint(RID p_font) const override;
virtual void font_set_oversampling(RID p_font_rid, real_t p_oversampling) override;
virtual real_t font_get_oversampling(RID p_font_rid) const override;
virtual void font_set_force_autohinter(RID p_font, bool p_enabeld) override;
virtual bool font_get_force_autohinter(RID p_font) const override;
virtual Array font_get_size_cache_list(RID p_font_rid) const override;
virtual void font_clear_size_cache(RID p_font_rid) override;
virtual void font_remove_size_cache(RID p_font_rid, const Vector2i &p_size) override;
virtual bool font_has_char(RID p_font, char32_t p_char) const override;
virtual String font_get_supported_chars(RID p_font) const override;
virtual void font_set_ascent(RID p_font_rid, int p_size, real_t p_ascent) override;
virtual real_t font_get_ascent(RID p_font_rid, int p_size) const override;
virtual bool font_has_outline(RID p_font) const override;
virtual float font_get_base_size(RID p_font) const override;
virtual void font_set_descent(RID p_font_rid, int p_size, real_t p_descent) override;
virtual real_t font_get_descent(RID p_font_rid, int p_size) const override;
virtual bool font_is_language_supported(RID p_font, const String &p_language) const override;
virtual void font_set_language_support_override(RID p_font, const String &p_language, bool p_supported) override;
virtual bool font_get_language_support_override(RID p_font, const String &p_language) override;
virtual void font_remove_language_support_override(RID p_font, const String &p_language) override;
Vector<String> font_get_language_support_overrides(RID p_font) override;
virtual void font_set_underline_position(RID p_font_rid, int p_size, real_t p_underline_position) override;
virtual real_t font_get_underline_position(RID p_font_rid, int p_size) const override;
virtual bool font_is_script_supported(RID p_font, const String &p_script) const override;
virtual void font_set_script_support_override(RID p_font, const String &p_script, bool p_supported) override;
virtual bool font_get_script_support_override(RID p_font, const String &p_script) override;
virtual void font_remove_script_support_override(RID p_font, const String &p_script) override;
Vector<String> font_get_script_support_overrides(RID p_font) override;
virtual void font_set_underline_thickness(RID p_font_rid, int p_size, real_t p_underline_thickness) override;
virtual real_t font_get_underline_thickness(RID p_font_rid, int p_size) const override;
virtual uint32_t font_get_glyph_index(RID p_font, char32_t p_char, char32_t p_variation_selector = 0x0000) const override;
virtual Vector2 font_get_glyph_advance(RID p_font, uint32_t p_index, int p_size) const override;
virtual Vector2 font_get_glyph_kerning(RID p_font, uint32_t p_index_a, uint32_t p_index_b, int p_size) const override;
virtual void font_set_scale(RID p_font_rid, int p_size, real_t p_scale) override;
virtual real_t font_get_scale(RID p_font_rid, int p_size) const override;
virtual Vector2 font_draw_glyph(RID p_font, RID p_canvas, int p_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color = Color(1, 1, 1)) const override;
virtual Vector2 font_draw_glyph_outline(RID p_font, RID p_canvas, int p_size, int p_outline_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color = Color(1, 1, 1)) const override;
virtual void font_set_spacing(RID p_font_rid, int p_size, SpacingType p_spacing, int p_value) override;
virtual int font_get_spacing(RID p_font_rid, int p_size, SpacingType p_spacing) const override;
virtual bool font_get_glyph_contours(RID p_font, int p_size, uint32_t p_index, Vector<Vector3> &r_points, Vector<int32_t> &r_contours, bool &r_orientation) const override;
virtual int font_get_texture_count(RID p_font_rid, const Vector2i &p_size) const override;
virtual void font_clear_textures(RID p_font_rid, const Vector2i &p_size) override;
virtual void font_remove_texture(RID p_font_rid, const Vector2i &p_size, int p_texture_index) override;
virtual float font_get_oversampling() const override;
virtual void font_set_oversampling(float p_oversampling) override;
virtual void font_set_texture_image(RID p_font_rid, const Vector2i &p_size, int p_texture_index, const Ref<Image> &p_image) override;
virtual Ref<Image> font_get_texture_image(RID p_font_rid, const Vector2i &p_size, int p_texture_index) const override;
virtual Vector<String> get_system_fonts() const override;
virtual void font_set_texture_offsets(RID p_font_rid, const Vector2i &p_size, int p_texture_index, const PackedInt32Array &p_offset) override;
virtual PackedInt32Array font_get_texture_offsets(RID p_font_rid, const Vector2i &p_size, int p_texture_index) const override;
virtual Array font_get_glyph_list(RID p_font_rid, const Vector2i &p_size) const override;
virtual void font_clear_glyphs(RID p_font_rid, const Vector2i &p_size) override;
virtual void font_remove_glyph(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph) override;
virtual Vector2 font_get_glyph_advance(RID p_font_rid, int p_size, int32_t p_glyph) const override;
virtual void font_set_glyph_advance(RID p_font_rid, int p_size, int32_t p_glyph, const Vector2 &p_advance) override;
virtual Vector2 font_get_glyph_offset(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph) const override;
virtual void font_set_glyph_offset(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph, const Vector2 &p_offset) override;
virtual Vector2 font_get_glyph_size(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph) const override;
virtual void font_set_glyph_size(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph, const Vector2 &p_gl_size) override;
virtual Rect2 font_get_glyph_uv_rect(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph) const override;
virtual void font_set_glyph_uv_rect(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph, const Rect2 &p_uv_rect) override;
virtual int font_get_glyph_texture_idx(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph) const override;
virtual void font_set_glyph_texture_idx(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph, int p_texture_idx) override;
virtual bool font_get_glyph_contours(RID p_font, int p_size, int32_t p_index, Vector<Vector3> &r_points, Vector<int32_t> &r_contours, bool &r_orientation) const override;
virtual Array font_get_kerning_list(RID p_font_rid, int p_size) const override;
virtual void font_clear_kerning_map(RID p_font_rid, int p_size) override;
virtual void font_remove_kerning(RID p_font_rid, int p_size, const Vector2i &p_glyph_pair) override;
virtual void font_set_kerning(RID p_font_rid, int p_size, const Vector2i &p_glyph_pair, const Vector2 &p_kerning) override;
virtual Vector2 font_get_kerning(RID p_font_rid, int p_size, const Vector2i &p_glyph_pair) const override;
virtual int32_t font_get_glyph_index(RID p_font_rid, int p_size, char32_t p_char, char32_t p_variation_selector = 0) const override;
virtual bool font_has_char(RID p_font_rid, char32_t p_char) const override;
virtual String font_get_supported_chars(RID p_font_rid) const override;
virtual void font_render_range(RID p_font, const Vector2i &p_size, char32_t p_start, char32_t p_end) override;
virtual void font_render_glyph(RID p_font_rid, const Vector2i &p_size, int32_t p_index) override;
virtual void font_draw_glyph(RID p_font, RID p_canvas, int p_size, const Vector2 &p_pos, int32_t p_index, const Color &p_color = Color(1, 1, 1)) const override;
virtual void font_draw_glyph_outline(RID p_font, RID p_canvas, int p_size, int p_outline_size, const Vector2 &p_pos, int32_t p_index, const Color &p_color = Color(1, 1, 1)) const override;
virtual bool font_is_language_supported(RID p_font_rid, const String &p_language) const override;
virtual void font_set_language_support_override(RID p_font_rid, const String &p_language, bool p_supported) override;
virtual bool font_get_language_support_override(RID p_font_rid, const String &p_language) override;
virtual void font_remove_language_support_override(RID p_font_rid, const String &p_language) override;
virtual Vector<String> font_get_language_support_overrides(RID p_font_rid) override;
virtual bool font_is_script_supported(RID p_font_rid, const String &p_script) const override;
virtual void font_set_script_support_override(RID p_font_rid, const String &p_script, bool p_supported) override;
virtual bool font_get_script_support_override(RID p_font_rid, const String &p_script) override;
virtual void font_remove_script_support_override(RID p_font_rid, const String &p_script) override;
virtual Vector<String> font_get_script_support_overrides(RID p_font_rid) override;
virtual Dictionary font_supported_feature_list(RID p_font_rid) const override;
virtual Dictionary font_supported_variation_list(RID p_font_rid) const override;
virtual real_t font_get_global_oversampling() const override;
virtual void font_set_global_oversampling(real_t p_oversampling) override;
/* Shaped text buffer interface */
@ -160,14 +212,14 @@ public:
virtual RID shaped_text_substr(RID p_shaped, int p_start, int p_length) const override;
virtual RID shaped_text_get_parent(RID p_shaped) const override;
virtual float shaped_text_fit_to_width(RID p_shaped, float p_width, uint8_t /*JustificationFlag*/ p_jst_flags = JUSTIFICATION_WORD_BOUND | JUSTIFICATION_KASHIDA) override;
virtual float shaped_text_tab_align(RID p_shaped, const Vector<float> &p_tab_stops) override;
virtual real_t shaped_text_fit_to_width(RID p_shaped, real_t p_width, uint8_t /*JustificationFlag*/ p_jst_flags = JUSTIFICATION_WORD_BOUND | JUSTIFICATION_KASHIDA) override;
virtual real_t shaped_text_tab_align(RID p_shaped, const Vector<real_t> &p_tab_stops) override;
virtual bool shaped_text_shape(RID p_shaped) override;
virtual bool shaped_text_update_breaks(RID p_shaped) override;
virtual bool shaped_text_update_justification_ops(RID p_shaped) override;
virtual void shaped_text_overrun_trim_to_width(RID p_shaped, float p_width, uint8_t p_trim_flags) override;
virtual void shaped_text_overrun_trim_to_width(RID p_shaped, real_t p_width, uint8_t p_trim_flags) override;
virtual bool shaped_text_is_ready(RID p_shaped) const override;
@ -176,18 +228,18 @@ public:
virtual Vector2i shaped_text_get_range(RID p_shaped) const override;
virtual Vector<Glyph> shaped_text_sort_logical(RID p_shaped) override;
virtual Vector<Vector2i> shaped_text_get_line_breaks_adv(RID p_shaped, const Vector<float> &p_width, int p_start = 0, bool p_once = true, uint8_t /*TextBreakFlag*/ p_break_flags = BREAK_MANDATORY | BREAK_WORD_BOUND) const override;
virtual Vector<Vector2i> shaped_text_get_line_breaks(RID p_shaped, float p_width, int p_start = 0, uint8_t p_break_flags = BREAK_MANDATORY | BREAK_WORD_BOUND) const override;
virtual Vector<Vector2i> shaped_text_get_line_breaks_adv(RID p_shaped, const Vector<real_t> &p_width, int p_start = 0, bool p_once = true, uint8_t /*TextBreakFlag*/ p_break_flags = BREAK_MANDATORY | BREAK_WORD_BOUND) const override;
virtual Vector<Vector2i> shaped_text_get_line_breaks(RID p_shaped, real_t p_width, int p_start = 0, uint8_t p_break_flags = BREAK_MANDATORY | BREAK_WORD_BOUND) const override;
virtual Vector<Vector2i> shaped_text_get_word_breaks(RID p_shaped, int p_grapheme_flags = GRAPHEME_IS_SPACE | GRAPHEME_IS_PUNCTUATION) const override;
virtual Array shaped_text_get_objects(RID p_shaped) const override;
virtual Rect2 shaped_text_get_object_rect(RID p_shaped, Variant p_key) const override;
virtual Size2 shaped_text_get_size(RID p_shaped) const override;
virtual float shaped_text_get_ascent(RID p_shaped) const override;
virtual float shaped_text_get_descent(RID p_shaped) const override;
virtual float shaped_text_get_width(RID p_shaped) const override;
virtual float shaped_text_get_underline_position(RID p_shaped) const override;
virtual float shaped_text_get_underline_thickness(RID p_shaped) const override;
virtual real_t shaped_text_get_ascent(RID p_shaped) const override;
virtual real_t shaped_text_get_descent(RID p_shaped) const override;
virtual real_t shaped_text_get_width(RID p_shaped) const override;
virtual real_t shaped_text_get_underline_position(RID p_shaped) const override;
virtual real_t shaped_text_get_underline_thickness(RID p_shaped) const override;
virtual String format_number(const String &p_string, const String &p_language = "") const override;
virtual String parse_number(const String &p_string, const String &p_language = "") const override;

66
modules/msdfgen/SCsub Normal file
View File

@ -0,0 +1,66 @@
#!/usr/bin/env python
Import("env")
Import("env_modules")
env_msdfgen = env_modules.Clone()
# Thirdparty source files
thirdparty_obj = []
if env["builtin_msdfgen"]:
env_msdfgen.disable_warnings()
thirdparty_dir = "#thirdparty/msdfgen/"
thirdparty_sources = [
"core/Contour.cpp",
"core/EdgeHolder.cpp",
"core/MSDFErrorCorrection.cpp",
"core/Projection.cpp",
"core/Scanline.cpp",
"core/Shape.cpp",
"core/SignedDistance.cpp",
"core/Vector2.cpp",
"core/contour-combiners.cpp",
"core/edge-coloring.cpp",
"core/edge-segments.cpp",
"core/edge-selectors.cpp",
"core/equation-solver.cpp",
"core/msdf-error-correction.cpp",
"core/msdfgen.cpp",
"core/rasterization.cpp",
"core/render-sdf.cpp",
"core/sdf-error-estimation.cpp",
"core/shape-description.cpp",
]
thirdparty_sources = [thirdparty_dir + file for file in thirdparty_sources]
env_msdfgen.Append(CPPPATH=["#thirdparty/freetype/include", "#thirdparty/msdfgen", "#thirdparty/nanosvg"])
lib = env_msdfgen.add_library("msdfgen_builtin", thirdparty_sources)
thirdparty_obj += lib
# Needs to be appended to arrive after libscene in the linker call,
# but we don't want it to arrive *after* system libs, so manual hack
# LIBS contains first SCons Library objects ("SCons.Node.FS.File object")
# and then plain strings for system library. We insert between the two.
inserted = False
for idx, linklib in enumerate(env["LIBS"]):
if isinstance(linklib, (str, bytes)): # first system lib such as "X11", otherwise SCons lib object
env["LIBS"].insert(idx, lib)
inserted = True
break
if not inserted:
env.Append(LIBS=[lib])
# Godot source files
module_obj = []
env_msdfgen.add_source_files(module_obj, "*.cpp")
env.modules_sources += module_obj
# Needed to force rebuilding the module files when the thirdparty library is updated.
env.Depends(module_obj, thirdparty_obj)

View File

@ -0,0 +1,6 @@
def can_build(env, platform):
return env.module_check_dependencies("msdfgen", ["freetype"])
def configure(env):
pass

View File

@ -0,0 +1,35 @@
/*************************************************************************/
/* register_types.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* 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. */
/*************************************************************************/
#include "register_types.h"
void register_msdfgen_types() {}
void unregister_msdfgen_types() {}

View File

@ -0,0 +1,37 @@
/*************************************************************************/
/* register_types.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* 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. */
/*************************************************************************/
#ifndef MSDFGEN_REGISTER_TYPES_H
#define MSDFGEN_REGISTER_TYPES_H
void register_msdfgen_types();
void unregister_msdfgen_types();
#endif // MSDFGEN_REGISTER_TYPES_H

View File

@ -38,7 +38,8 @@ def make_icu_data(target, source, env):
# Thirdparty source files
thirdparty_obj = []
freetype_enabled = env.module_check_dependencies("text_server_adv", ["freetype"])
freetype_enabled = env.module_check_dependencies("text_server_adv", ["freetype"], True)
msdngen_enabled = env.module_check_dependencies("text_server_adv", ["msdfgen"], True)
if env["builtin_harfbuzz"]:
env_harfbuzz = env_modules.Clone()
@ -509,6 +510,13 @@ env_text_server_adv.Append(
]
)
if msdngen_enabled:
env_text_server_adv.Append(
CPPPATH=[
"#thirdparty/msdfgen",
]
)
if freetype_enabled:
env_text_server_adv.Append(
CPPPATH=[

View File

@ -1,585 +0,0 @@
/*************************************************************************/
/* bitmap_font_adv.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* 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. */
/*************************************************************************/
#include "bitmap_font_adv.h"
/*************************************************************************/
/* hb_bmp_font_t HarfBuzz Bitmap font interface */
/*************************************************************************/
struct hb_bmp_font_t {
BitmapFontDataAdvanced *face = nullptr;
float font_size = 0.0;
bool unref = false; /* Whether to destroy bm_face when done. */
};
static hb_bmp_font_t *_hb_bmp_font_create(BitmapFontDataAdvanced *p_face, float p_font_size, bool p_unref) {
hb_bmp_font_t *bm_font = reinterpret_cast<hb_bmp_font_t *>(calloc(1, sizeof(hb_bmp_font_t)));
if (!bm_font) {
return nullptr;
}
bm_font->face = p_face;
bm_font->font_size = p_font_size;
bm_font->unref = p_unref;
return bm_font;
}
static void _hb_bmp_font_destroy(void *data) {
hb_bmp_font_t *bm_font = reinterpret_cast<hb_bmp_font_t *>(data);
free(bm_font);
}
static hb_bool_t hb_bmp_get_nominal_glyph(hb_font_t *font, void *font_data, hb_codepoint_t unicode, hb_codepoint_t *glyph, void *user_data) {
const hb_bmp_font_t *bm_font = reinterpret_cast<const hb_bmp_font_t *>(font_data);
if (!bm_font->face) {
return false;
}
if (!bm_font->face->has_char(unicode)) {
if (bm_font->face->has_char(0xF000u + unicode)) {
*glyph = 0xF000u + unicode;
return true;
} else {
return false;
}
}
*glyph = unicode;
return true;
}
static hb_position_t hb_bmp_get_glyph_h_advance(hb_font_t *font, void *font_data, hb_codepoint_t glyph, void *user_data) {
const hb_bmp_font_t *bm_font = reinterpret_cast<const hb_bmp_font_t *>(font_data);
if (!bm_font->face) {
return 0;
}
if (!bm_font->face->has_char(glyph)) {
return 0;
}
return bm_font->face->get_advance(glyph, bm_font->font_size).x * 64;
}
static hb_position_t hb_bmp_get_glyph_v_advance(hb_font_t *font, void *font_data, hb_codepoint_t glyph, void *user_data) {
const hb_bmp_font_t *bm_font = reinterpret_cast<const hb_bmp_font_t *>(font_data);
if (!bm_font->face) {
return 0;
}
if (!bm_font->face->has_char(glyph)) {
return 0;
}
return -bm_font->face->get_advance(glyph, bm_font->font_size).y * 64;
}
static hb_position_t hb_bmp_get_glyph_h_kerning(hb_font_t *font, void *font_data, hb_codepoint_t left_glyph, hb_codepoint_t right_glyph, void *user_data) {
const hb_bmp_font_t *bm_font = reinterpret_cast<const hb_bmp_font_t *>(font_data);
if (!bm_font->face) {
return 0;
}
if (!bm_font->face->has_char(left_glyph)) {
return 0;
}
if (!bm_font->face->has_char(right_glyph)) {
return 0;
}
return bm_font->face->get_kerning(left_glyph, right_glyph, bm_font->font_size).x * 64;
}
static hb_bool_t hb_bmp_get_glyph_v_origin(hb_font_t *font, void *font_data, hb_codepoint_t glyph, hb_position_t *x, hb_position_t *y, void *user_data) {
const hb_bmp_font_t *bm_font = reinterpret_cast<const hb_bmp_font_t *>(font_data);
if (!bm_font->face) {
return false;
}
if (!bm_font->face->has_char(glyph)) {
return false;
}
*x = bm_font->face->get_advance(glyph, bm_font->font_size).x * 32;
*y = bm_font->face->get_ascent(bm_font->font_size) * 64;
return true;
}
static hb_bool_t hb_bmp_get_glyph_extents(hb_font_t *font, void *font_data, hb_codepoint_t glyph, hb_glyph_extents_t *extents, void *user_data) {
const hb_bmp_font_t *bm_font = reinterpret_cast<const hb_bmp_font_t *>(font_data);
if (!bm_font->face) {
return false;
}
if (!bm_font->face->has_char(glyph)) {
return false;
}
extents->x_bearing = 0;
extents->y_bearing = 0;
extents->width = bm_font->face->get_size(glyph, bm_font->font_size).x * 64;
extents->height = bm_font->face->get_size(glyph, bm_font->font_size).y * 64;
return true;
}
static hb_bool_t hb_bmp_get_font_h_extents(hb_font_t *font, void *font_data, hb_font_extents_t *metrics, void *user_data) {
const hb_bmp_font_t *bm_font = reinterpret_cast<const hb_bmp_font_t *>(font_data);
if (!bm_font->face) {
return false;
}
metrics->ascender = bm_font->face->get_ascent(bm_font->font_size);
metrics->descender = bm_font->face->get_descent(bm_font->font_size);
metrics->line_gap = 0;
return true;
}
static hb_font_funcs_t *funcs = nullptr;
void hb_bmp_create_font_funcs() {
funcs = hb_font_funcs_create();
hb_font_funcs_set_font_h_extents_func(funcs, hb_bmp_get_font_h_extents, nullptr, nullptr);
//hb_font_funcs_set_font_v_extents_func (funcs, hb_bmp_get_font_v_extents, nullptr, nullptr);
hb_font_funcs_set_nominal_glyph_func(funcs, hb_bmp_get_nominal_glyph, nullptr, nullptr);
//hb_font_funcs_set_variation_glyph_func (funcs, hb_bmp_get_variation_glyph, nullptr, nullptr);
hb_font_funcs_set_glyph_h_advance_func(funcs, hb_bmp_get_glyph_h_advance, nullptr, nullptr);
hb_font_funcs_set_glyph_v_advance_func(funcs, hb_bmp_get_glyph_v_advance, nullptr, nullptr);
//hb_font_funcs_set_glyph_h_origin_func(funcs, hb_bmp_get_glyph_h_origin, nullptr, nullptr);
hb_font_funcs_set_glyph_v_origin_func(funcs, hb_bmp_get_glyph_v_origin, nullptr, nullptr);
hb_font_funcs_set_glyph_h_kerning_func(funcs, hb_bmp_get_glyph_h_kerning, nullptr, nullptr);
//hb_font_funcs_set_glyph_v_kerning_func (funcs, hb_bmp_get_glyph_v_kerning, nullptr, nullptr);
hb_font_funcs_set_glyph_extents_func(funcs, hb_bmp_get_glyph_extents, nullptr, nullptr);
//hb_font_funcs_set_glyph_contour_point_func (funcs, hb_bmp_get_glyph_contour_point, nullptr, nullptr);
//hb_font_funcs_set_glyph_name_func (funcs, hb_bmp_get_glyph_name, nullptr, nullptr);
//hb_font_funcs_set_glyph_from_name_func (funcs, hb_bmp_get_glyph_from_name, nullptr, nullptr);
hb_font_funcs_make_immutable(funcs);
}
void hb_bmp_free_font_funcs() {
if (funcs != nullptr) {
hb_font_funcs_destroy(funcs);
funcs = nullptr;
}
}
static void _hb_bmp_font_set_funcs(hb_font_t *p_font, BitmapFontDataAdvanced *p_face, int p_size, bool p_unref) {
hb_font_set_funcs(p_font, funcs, _hb_bmp_font_create(p_face, p_size, p_unref), _hb_bmp_font_destroy);
}
hb_font_t *hb_bmp_font_create(BitmapFontDataAdvanced *p_face, int p_size, hb_destroy_func_t p_destroy) {
hb_font_t *font;
hb_face_t *face = hb_face_create(nullptr, 0);
font = hb_font_create(face);
hb_face_destroy(face);
_hb_bmp_font_set_funcs(font, p_face, p_size, false);
return font;
}
/*************************************************************************/
/* BitmapFontDataAdvanced */
/*************************************************************************/
Error BitmapFontDataAdvanced::load_from_file(const String &p_filename, int p_base_size) {
_THREAD_SAFE_METHOD_
//fnt format used by angelcode bmfont
//https://www.angelcode.com/products/bmfont/
FileAccess *f = FileAccess::open(p_filename, FileAccess::READ);
ERR_FAIL_COND_V_MSG(!f, ERR_FILE_NOT_FOUND, "Can't open font: " + p_filename + ".");
while (true) {
String line = f->get_line();
int delimiter = line.find(" ");
String type = line.substr(0, delimiter);
int pos = delimiter + 1;
Map<String, String> keys;
while (pos < line.size() && line[pos] == ' ') {
pos++;
}
while (pos < line.size()) {
int eq = line.find("=", pos);
if (eq == -1) {
break;
}
String key = line.substr(pos, eq - pos);
int end = -1;
String value;
if (line[eq + 1] == '"') {
end = line.find("\"", eq + 2);
if (end == -1) {
break;
}
value = line.substr(eq + 2, end - 1 - eq - 1);
pos = end + 1;
} else {
end = line.find(" ", eq + 1);
if (end == -1) {
end = line.size();
}
value = line.substr(eq + 1, end - eq);
pos = end;
}
while (pos < line.size() && line[pos] == ' ') {
pos++;
}
keys[key] = value;
}
if (type == "info") {
if (keys.has("size")) {
base_size = keys["size"].to_int();
}
} else if (type == "common") {
if (keys.has("lineHeight")) {
height = keys["lineHeight"].to_int();
}
if (keys.has("base")) {
ascent = keys["base"].to_int();
}
} else if (type == "page") {
if (keys.has("file")) {
String base_dir = p_filename.get_base_dir();
String file = base_dir.plus_file(keys["file"]);
if (RenderingServer::get_singleton() != nullptr) {
Ref<Texture2D> tex = ResourceLoader::load(file);
if (tex.is_null()) {
ERR_PRINT("Can't load font texture!");
} else {
ERR_FAIL_COND_V_MSG(tex.is_null(), ERR_FILE_CANT_READ, "It's not a reference to a valid Texture object.");
textures.push_back(tex);
}
}
}
} else if (type == "char") {
Character c;
char32_t idx = 0;
if (keys.has("id")) {
idx = keys["id"].to_int();
}
if (keys.has("x")) {
c.rect.position.x = keys["x"].to_int();
}
if (keys.has("y")) {
c.rect.position.y = keys["y"].to_int();
}
if (keys.has("width")) {
c.rect.size.width = keys["width"].to_int();
}
if (keys.has("height")) {
c.rect.size.height = keys["height"].to_int();
}
if (keys.has("xoffset")) {
c.align.x = keys["xoffset"].to_int();
}
if (keys.has("yoffset")) {
c.align.y = keys["yoffset"].to_int();
}
if (keys.has("page")) {
c.texture_idx = keys["page"].to_int();
}
if (keys.has("xadvance")) {
c.advance.x = keys["xadvance"].to_int();
}
if (keys.has("yadvance")) {
c.advance.x = keys["yadvance"].to_int();
}
if (c.advance.x < 0) {
c.advance.x = c.rect.size.width + 1;
}
if (c.advance.y < 0) {
c.advance.y = c.rect.size.height + 1;
}
char_map[idx] = c;
} else if (type == "kerning") {
KerningPairKey kpk;
float k = 0.0;
if (keys.has("first")) {
kpk.A = keys["first"].to_int();
}
if (keys.has("second")) {
kpk.B = keys["second"].to_int();
}
if (keys.has("amount")) {
k = keys["amount"].to_int();
}
kerning_map[kpk] = k;
}
if (f->eof_reached()) {
break;
}
}
if (base_size == 0) {
base_size = height;
}
if (hb_handle) {
hb_font_destroy(hb_handle);
}
hb_handle = hb_bmp_font_create(this, base_size, nullptr);
valid = true;
memdelete(f);
return OK;
}
Error BitmapFontDataAdvanced::bitmap_new(float p_height, float p_ascent, int p_base_size) {
height = p_height;
ascent = p_ascent;
base_size = p_base_size;
if (base_size == 0) {
base_size = height;
}
char_map.clear();
textures.clear();
kerning_map.clear();
if (hb_handle) {
hb_font_destroy(hb_handle);
}
hb_handle = hb_bmp_font_create(this, base_size, nullptr);
valid = true;
return OK;
}
void BitmapFontDataAdvanced::bitmap_add_texture(const Ref<Texture> &p_texture) {
ERR_FAIL_COND(!valid);
ERR_FAIL_COND_MSG(p_texture.is_null(), "It's not a reference to a valid Texture object.");
textures.push_back(p_texture);
}
void BitmapFontDataAdvanced::bitmap_add_char(char32_t p_char, int p_texture_idx, const Rect2 &p_rect, const Size2 &p_align, float p_advance) {
ERR_FAIL_COND(!valid);
Character chr;
chr.rect = p_rect;
chr.texture_idx = p_texture_idx;
if (p_advance < 0) {
chr.advance.x = chr.rect.size.x;
} else {
chr.advance.x = p_advance;
}
chr.align = p_align;
char_map[p_char] = chr;
}
void BitmapFontDataAdvanced::bitmap_add_kerning_pair(char32_t p_A, char32_t p_B, int p_kerning) {
ERR_FAIL_COND(!valid);
KerningPairKey kpk;
kpk.A = p_A;
kpk.B = p_B;
if (p_kerning == 0 && kerning_map.has(kpk)) {
kerning_map.erase(kpk);
} else {
kerning_map[kpk] = p_kerning;
}
}
float BitmapFontDataAdvanced::get_height(int p_size) const {
ERR_FAIL_COND_V(!valid, 0.f);
return height * (float(p_size) / float(base_size));
}
float BitmapFontDataAdvanced::get_ascent(int p_size) const {
ERR_FAIL_COND_V(!valid, 0.f);
return ascent * (float(p_size) / float(base_size));
}
float BitmapFontDataAdvanced::get_descent(int p_size) const {
ERR_FAIL_COND_V(!valid, 0.f);
return (height - ascent) * (float(p_size) / float(base_size));
}
float BitmapFontDataAdvanced::get_underline_position(int p_size) const {
ERR_FAIL_COND_V(!valid, 0.f);
return 2 * (float(p_size) / float(base_size));
}
float BitmapFontDataAdvanced::get_underline_thickness(int p_size) const {
ERR_FAIL_COND_V(!valid, 0.f);
return 1 * (float(p_size) / float(base_size));
}
void BitmapFontDataAdvanced::set_distance_field_hint(bool p_distance_field) {
distance_field_hint = p_distance_field;
}
bool BitmapFontDataAdvanced::get_distance_field_hint() const {
return distance_field_hint;
}
float BitmapFontDataAdvanced::get_base_size() const {
return base_size;
}
hb_font_t *BitmapFontDataAdvanced::get_hb_handle(int p_size) {
_THREAD_SAFE_METHOD_
ERR_FAIL_COND_V(!valid, nullptr);
return hb_handle;
}
bool BitmapFontDataAdvanced::has_char(char32_t p_char) const {
_THREAD_SAFE_METHOD_
ERR_FAIL_COND_V(!valid, false);
return char_map.has(p_char);
}
String BitmapFontDataAdvanced::get_supported_chars() const {
_THREAD_SAFE_METHOD_
ERR_FAIL_COND_V(!valid, String());
String chars;
const uint32_t *k = nullptr;
while ((k = char_map.next(k))) {
chars += char32_t(*k);
}
return chars;
}
Vector2 BitmapFontDataAdvanced::get_advance(uint32_t p_char, int p_size) const {
_THREAD_SAFE_METHOD_
ERR_FAIL_COND_V(!valid, Vector2());
const Character *c = char_map.getptr(p_char);
ERR_FAIL_COND_V(c == nullptr, Vector2());
return c->advance * (float(p_size) / float(base_size));
}
Vector2 BitmapFontDataAdvanced::get_align(uint32_t p_char, int p_size) const {
_THREAD_SAFE_METHOD_
ERR_FAIL_COND_V(!valid, Vector2());
const Character *c = char_map.getptr(p_char);
ERR_FAIL_COND_V(c == nullptr, Vector2());
return c->align * (float(p_size) / float(base_size));
}
Vector2 BitmapFontDataAdvanced::get_size(uint32_t p_char, int p_size) const {
_THREAD_SAFE_METHOD_
ERR_FAIL_COND_V(!valid, Vector2());
const Character *c = char_map.getptr(p_char);
ERR_FAIL_COND_V(c == nullptr, Vector2());
return c->rect.size * (float(p_size) / float(base_size));
}
float BitmapFontDataAdvanced::get_font_scale(int p_size) const {
return float(p_size) / float(base_size);
}
Vector2 BitmapFontDataAdvanced::get_kerning(uint32_t p_char, uint32_t p_next, int p_size) const {
_THREAD_SAFE_METHOD_
ERR_FAIL_COND_V(!valid, Vector2());
KerningPairKey kpk;
kpk.A = p_char;
kpk.B = p_next;
const Map<KerningPairKey, int>::Element *E = kerning_map.find(kpk);
if (E) {
return Vector2(-E->get() * (float(p_size) / float(base_size)), 0.f);
} else {
return Vector2();
}
}
Vector2 BitmapFontDataAdvanced::draw_glyph(RID p_canvas, int p_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color) const {
_THREAD_SAFE_METHOD_
if (p_index == 0) {
return Vector2();
}
ERR_FAIL_COND_V(!valid, Vector2());
const Character *c = char_map.getptr(p_index);
ERR_FAIL_COND_V(c == nullptr, Vector2());
ERR_FAIL_COND_V(c->texture_idx < -1 || c->texture_idx >= textures.size(), Vector2());
if (c->texture_idx != -1) {
Point2i cpos = p_pos;
cpos += (c->align + Vector2(0, -ascent)) * (float(p_size) / float(base_size));
Size2i csize = c->rect.size * (float(p_size) / float(base_size));
if (RenderingServer::get_singleton() != nullptr) {
//if (distance_field_hint) { // Not implemented.
// RenderingServer::get_singleton()->canvas_item_set_distance_field_mode(p_canvas, true);
//}
RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, Rect2(cpos, csize), textures[c->texture_idx]->get_rid(), c->rect, p_color, false, false);
//if (distance_field_hint) {
// RenderingServer::get_singleton()->canvas_item_set_distance_field_mode(p_canvas, false);
//}
}
}
return c->advance * (float(p_size) / float(base_size));
}
Vector2 BitmapFontDataAdvanced::draw_glyph_outline(RID p_canvas, int p_size, int p_outline_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color) const {
_THREAD_SAFE_METHOD_
if (p_index == 0) {
return Vector2();
}
ERR_FAIL_COND_V(!valid, Vector2());
const Character *c = char_map.getptr(p_index);
ERR_FAIL_COND_V(c == nullptr, Vector2());
ERR_FAIL_COND_V(c->texture_idx < -1 || c->texture_idx >= textures.size(), Vector2());
// Not supported, return advance for compatibility.
return c->advance * (float(p_size) / float(base_size));
}
BitmapFontDataAdvanced::~BitmapFontDataAdvanced() {
if (hb_handle) {
hb_font_destroy(hb_handle);
}
}

View File

@ -1,123 +0,0 @@
/*************************************************************************/
/* bitmap_font_adv.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* 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. */
/*************************************************************************/
#ifndef BITMAP_FONT_ADV_H
#define BITMAP_FONT_ADV_H
#include "font_adv.h"
void hb_bmp_create_font_funcs();
void hb_bmp_free_font_funcs();
struct BitmapFontDataAdvanced : public FontDataAdvanced {
_THREAD_SAFE_CLASS_
private:
Vector<Ref<Texture2D>> textures;
struct Character {
int texture_idx = 0;
Rect2 rect;
Vector2 align;
Vector2 advance = Vector2(-1, -1);
};
struct KerningPairKey {
union {
struct {
uint32_t A, B;
};
uint64_t pair = 0;
};
_FORCE_INLINE_ bool operator<(const KerningPairKey &p_r) const { return pair < p_r.pair; }
};
HashMap<uint32_t, Character> char_map;
Map<KerningPairKey, int> kerning_map;
hb_font_t *hb_handle = nullptr;
float height = 0.f;
float ascent = 0.f;
int base_size = 0;
bool distance_field_hint = false;
public:
virtual void clear_cache() override{};
virtual Error load_from_file(const String &p_filename, int p_base_size) override;
virtual Error bitmap_new(float p_height, float p_ascent, int p_base_size) override;
virtual void bitmap_add_texture(const Ref<Texture> &p_texture) override;
virtual void bitmap_add_char(char32_t p_char, int p_texture_idx, const Rect2 &p_rect, const Size2 &p_align, float p_advance) override;
virtual void bitmap_add_kerning_pair(char32_t p_A, char32_t p_B, int p_kerning) override;
virtual float get_height(int p_size) const override;
virtual float get_ascent(int p_size) const override;
virtual float get_descent(int p_size) const override;
virtual float get_underline_position(int p_size) const override;
virtual float get_underline_thickness(int p_size) const override;
virtual void set_antialiased(bool p_antialiased) override{};
virtual bool get_antialiased() const override { return false; };
virtual void set_hinting(TextServer::Hinting p_hinting) override{};
virtual TextServer::Hinting get_hinting() const override { return TextServer::HINTING_NONE; };
virtual void set_distance_field_hint(bool p_distance_field) override;
virtual bool get_distance_field_hint() const override;
virtual void set_force_autohinter(bool p_enabeld) override{};
virtual bool get_force_autohinter() const override { return false; };
virtual bool has_outline() const override { return false; };
virtual float get_base_size() const override;
virtual float get_font_scale(int p_size) const override;
virtual hb_font_t *get_hb_handle(int p_size) override;
virtual bool has_char(char32_t p_char) const override;
virtual String get_supported_chars() const override;
virtual Vector2 get_advance(uint32_t p_char, int p_size) const override;
Vector2 get_align(uint32_t p_char, int p_size) const;
Vector2 get_size(uint32_t p_char, int p_size) const;
virtual Vector2 get_kerning(uint32_t p_char, uint32_t p_next, int p_size) const override;
virtual uint32_t get_glyph_index(char32_t p_char, char32_t p_variation_selector) const override { return (uint32_t)p_char; };
virtual Vector2 draw_glyph(RID p_canvas, int p_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color) const override;
virtual Vector2 draw_glyph_outline(RID p_canvas, int p_size, int p_outline_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color) const override;
virtual ~BitmapFontDataAdvanced();
};
#endif // BITMAP_FONT_ADV_H

File diff suppressed because it is too large Load Diff

View File

@ -1,195 +0,0 @@
/*************************************************************************/
/* dynamic_font_adv.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* 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. */
/*************************************************************************/
#ifndef DYNAMIC_FONT_ADV_H
#define DYNAMIC_FONT_ADV_H
#include "font_adv.h"
#include "modules/modules_enabled.gen.h"
#ifdef MODULE_FREETYPE_ENABLED
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_TRUETYPE_TABLES_H
#include <hb-ft.h>
#include <hb-ot.h>
struct DynamicFontDataAdvanced : public FontDataAdvanced {
_THREAD_SAFE_CLASS_
private:
struct CharTexture {
Vector<uint8_t> imgdata;
int texture_size = 0;
Vector<int> offsets;
Ref<ImageTexture> texture;
};
struct Character {
bool found = false;
int texture_idx = 0;
Rect2 rect;
Rect2 rect_uv;
Vector2 align;
Vector2 advance = Vector2(-1, -1);
static Character not_found();
};
struct TexturePosition {
int index = 0;
int x = 0;
int y = 0;
};
struct CacheID {
union {
struct {
uint32_t size : 16;
uint32_t outline_size : 16;
};
uint32_t key = 0;
};
bool operator<(CacheID right) const {
return key < right.key;
}
};
struct DataAtSize {
FT_Face face = nullptr;
TT_OS2 *os2 = nullptr;
FT_StreamRec stream;
int size = 0;
float scale_color_font = 1.f;
float ascent = 0.0;
float descent = 0.0;
float underline_position = 0.0;
float underline_thickness = 0.0;
Vector<CharTexture> textures;
HashMap<uint32_t, Character> glyph_map;
hb_font_t *hb_handle = nullptr;
~DataAtSize() {
if (hb_handle != nullptr) {
hb_font_destroy(hb_handle);
}
if (face != nullptr) {
FT_Done_Face(face);
}
}
};
FT_Library library = nullptr;
// Source data.
const uint8_t *font_mem = nullptr;
int font_mem_size = 0;
String font_path;
Vector<uint8_t> font_mem_cache;
Map<int32_t, double> variations;
float rect_margin = 1.f;
int base_size = 16;
float oversampling = 1.f;
bool antialiased = true;
bool force_autohinter = false;
TextServer::Hinting hinting = TextServer::HINTING_LIGHT;
Map<CacheID, DataAtSize *> size_cache;
Map<CacheID, DataAtSize *> size_cache_outline;
DataAtSize *get_data_for_size(int p_size, int p_outline_size = 0);
TexturePosition find_texture_pos_for_glyph(DataAtSize *p_data, int p_color_size, Image::Format p_image_format, int p_width, int p_height);
Character bitmap_to_character(DataAtSize *p_data, FT_Bitmap bitmap, int yofs, int xofs, const Vector2 &advance);
_FORCE_INLINE_ void update_glyph(int p_size, uint32_t p_index);
_FORCE_INLINE_ void update_glyph_outline(int p_size, int p_outline_size, uint32_t p_index);
public:
virtual void clear_cache() override;
virtual Error load_from_file(const String &p_filename, int p_base_size) override;
virtual Error load_from_memory(const uint8_t *p_data, size_t p_size, int p_base_size) override;
virtual float get_height(int p_size) const override;
virtual float get_ascent(int p_size) const override;
virtual float get_descent(int p_size) const override;
virtual Dictionary get_feature_list() const override;
virtual Dictionary get_variation_list() const override;
virtual void set_variation(const String &p_name, double p_value) override;
virtual double get_variation(const String &p_name) const override;
virtual float get_underline_position(int p_size) const override;
virtual float get_underline_thickness(int p_size) const override;
virtual void set_antialiased(bool p_antialiased) override;
virtual bool get_antialiased() const override;
virtual void set_hinting(TextServer::Hinting p_hinting) override;
virtual TextServer::Hinting get_hinting() const override;
virtual void set_force_autohinter(bool p_enabled) override;
virtual bool get_force_autohinter() const override;
virtual void set_distance_field_hint(bool p_distance_field) override{};
virtual bool get_distance_field_hint() const override { return false; };
virtual bool has_outline() const override;
virtual float get_base_size() const override;
virtual bool is_script_supported(uint32_t p_script) const override;
virtual bool has_char(char32_t p_char) const override;
virtual String get_supported_chars() const override;
virtual float get_font_scale(int p_size) const override;
virtual hb_font_t *get_hb_handle(int p_size) override;
virtual uint32_t get_glyph_index(char32_t p_char, char32_t p_variation_selector) const override;
virtual Vector2 get_advance(uint32_t p_index, int p_size) const override;
virtual Vector2 get_kerning(uint32_t p_char, uint32_t p_next, int p_size) const override;
virtual Vector2 draw_glyph(RID p_canvas, int p_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color) const override;
virtual Vector2 draw_glyph_outline(RID p_canvas, int p_size, int p_outline_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color) const override;
virtual bool get_glyph_contours(int p_size, uint32_t p_index, Vector<Vector3> &r_points, Vector<int32_t> &r_contours, bool &r_orientation) const override;
virtual ~DynamicFontDataAdvanced() override;
};
#endif // MODULE_FREETYPE_ENABLED
#endif // DYNAMIC_FONT_ADV_H

View File

@ -1,115 +0,0 @@
/*************************************************************************/
/* font_adv.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* 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. */
/*************************************************************************/
#ifndef FONT_ADV_H
#define FONT_ADV_H
#include "servers/text_server.h"
#include <hb.h>
struct FontDataAdvanced {
Map<String, bool> lang_support_overrides;
Map<String, bool> script_support_overrides;
bool valid = false;
int spacing_space = 0;
int spacing_glyph = 0;
virtual void clear_cache() = 0;
virtual Error load_from_file(const String &p_filename, int p_base_size) { return ERR_CANT_CREATE; };
virtual Error load_from_memory(const uint8_t *p_data, size_t p_size, int p_base_size) { return ERR_CANT_CREATE; };
virtual Error bitmap_new(float p_height, float p_ascent, int p_base_size) { return ERR_CANT_CREATE; };
virtual void bitmap_add_texture(const Ref<Texture> &p_texture) { ERR_FAIL_MSG("Not supported."); };
virtual void bitmap_add_char(char32_t p_char, int p_texture_idx, const Rect2 &p_rect, const Size2 &p_align, float p_advance) { ERR_FAIL_MSG("Not supported."); };
virtual void bitmap_add_kerning_pair(char32_t p_A, char32_t p_B, int p_kerning) { ERR_FAIL_MSG("Not supported."); };
virtual float get_height(int p_size) const = 0;
virtual float get_ascent(int p_size) const = 0;
virtual float get_descent(int p_size) const = 0;
virtual Dictionary get_feature_list() const { return Dictionary(); };
virtual Dictionary get_variation_list() const { return Dictionary(); };
virtual void set_variation(const String &p_name, double p_value){};
virtual double get_variation(const String &p_name) const { return 0; };
virtual float get_underline_position(int p_size) const = 0;
virtual float get_underline_thickness(int p_size) const = 0;
virtual int get_spacing_space() const { return spacing_space; };
virtual void set_spacing_space(int p_value) {
spacing_space = p_value;
clear_cache();
};
virtual int get_spacing_glyph() const { return spacing_glyph; };
virtual void set_spacing_glyph(int p_value) {
spacing_glyph = p_value;
clear_cache();
};
virtual void set_antialiased(bool p_antialiased) = 0;
virtual bool get_antialiased() const = 0;
virtual void set_hinting(TextServer::Hinting p_hinting) = 0;
virtual TextServer::Hinting get_hinting() const = 0;
virtual void set_distance_field_hint(bool p_distance_field) = 0;
virtual bool get_distance_field_hint() const = 0;
virtual void set_force_autohinter(bool p_enabeld) = 0;
virtual bool get_force_autohinter() const = 0;
virtual bool has_outline() const = 0;
virtual float get_base_size() const = 0;
virtual bool is_lang_supported(const String &p_lang) const { return true; };
virtual bool is_script_supported(uint32_t p_script) const { return true; };
virtual bool has_char(char32_t p_char) const = 0;
virtual String get_supported_chars() const = 0;
virtual float get_font_scale(int p_size) const { return 1.0f; };
virtual hb_font_t *get_hb_handle(int p_size) = 0;
virtual uint32_t get_glyph_index(char32_t p_char, char32_t p_variation_selector) const = 0;
virtual Vector2 get_advance(uint32_t p_char, int p_size) const = 0;
virtual Vector2 get_kerning(uint32_t p_char, uint32_t p_next, int p_size) const = 0;
virtual Vector2 draw_glyph(RID p_canvas, int p_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color) const = 0;
virtual Vector2 draw_glyph_outline(RID p_canvas, int p_size, int p_outline_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color) const = 0;
virtual bool get_glyph_contours(int p_size, uint32_t p_index, Vector<Vector3> &r_points, Vector<int32_t> &r_contours, bool &r_orientation) const { return false; };
virtual ~FontDataAdvanced(){};
};
#endif // FONT_ADV_H

File diff suppressed because it is too large Load Diff

View File

@ -39,6 +39,7 @@
#include "servers/text_server.h"
#include "core/templates/rid_owner.h"
#include "core/templates/thread_work_pool.h"
#include "scene/resources/texture.h"
#include "script_iterator.h"
@ -53,11 +54,24 @@
#include <unicode/ustring.h>
#include <unicode/utypes.h>
#include "modules/modules_enabled.gen.h"
#ifdef MODULE_FREETYPE_ENABLED
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_TRUETYPE_TABLES_H
#include FT_STROKER_H
#include FT_ADVANCES_H
#include FT_MULTIPLE_MASTERS_H
#include FT_BBOX_H
#include <hb-ft.h>
#include <hb-ot.h>
#endif
#include <hb-icu.h>
#include <hb.h>
#include "font_adv.h"
class TextServerAdvanced : public TextServer {
GDCLASS(TextServerAdvanced, TextServer);
_THREAD_SAFE_CLASS_
@ -65,8 +79,149 @@ class TextServerAdvanced : public TextServer {
static String interface_name;
static uint32_t interface_features;
// ICU support data.
uint8_t *icu_data = nullptr;
// Font cache data.
#ifdef MODULE_FREETYPE_ENABLED
mutable FT_Library library = nullptr;
#endif
const int rect_range = 2;
struct FontTexture {
Image::Format format;
PackedByteArray imgdata;
int texture_w = 0;
int texture_h = 0;
PackedInt32Array offsets;
Ref<ImageTexture> texture;
};
struct FontTexturePosition {
int index = 0;
int x = 0;
int y = 0;
};
struct FontGlyph {
bool found = false;
int texture_idx = -1;
Rect2 rect;
Rect2 uv_rect;
Vector2 advance;
};
struct FontDataForSizeAdvanced {
real_t ascent = 0.f;
real_t descent = 0.f;
real_t underline_position = 0.f;
real_t underline_thickness = 0.f;
real_t scale = 1.f;
real_t oversampling = 1.f;
int spacing_glyph = 0;
int spacing_space = 0;
Vector2i size;
Vector<FontTexture> textures;
HashMap<int32_t, FontGlyph> glyph_map;
Map<Vector2i, Vector2> kerning_map;
hb_font_t *hb_handle = nullptr;
#ifdef MODULE_FREETYPE_ENABLED
FT_Face face = nullptr;
FT_StreamRec stream;
#endif
~FontDataForSizeAdvanced() {
if (hb_handle != nullptr) {
hb_font_destroy(hb_handle);
}
#ifdef MODULE_FREETYPE_ENABLED
if (face != nullptr) {
FT_Done_Face(face);
}
#endif
}
};
struct FontDataAdvanced {
Mutex mutex;
bool antialiased = true;
bool msdf = false;
int msdf_range = 14;
int msdf_source_size = 48;
int fixed_size = 0;
bool force_autohinter = false;
TextServer::Hinting hinting = TextServer::HINTING_LIGHT;
Dictionary variation_coordinates;
real_t oversampling = 0.f;
Map<Vector2i, FontDataForSizeAdvanced *> cache;
bool face_init = false;
Set<uint32_t> supported_scripts;
Dictionary supported_features;
Dictionary supported_varaitions;
// Language/script support override.
Map<String, bool> language_support_overrides;
Map<String, bool> script_support_overrides;
PackedByteArray data;
const uint8_t *data_ptr;
size_t data_size;
mutable ThreadWorkPool work_pool;
~FontDataAdvanced() {
work_pool.finish();
for (const Map<Vector2i, FontDataForSizeAdvanced *>::Element *E = cache.front(); E; E = E->next()) {
memdelete(E->get());
}
cache.clear();
}
};
_FORCE_INLINE_ FontTexturePosition find_texture_pos_for_glyph(FontDataForSizeAdvanced *p_data, int p_color_size, Image::Format p_image_format, int p_width, int p_height) const;
#ifdef MODULE_MSDFGEN_ENABLED
_FORCE_INLINE_ FontGlyph rasterize_msdf(FontDataAdvanced *p_font_data, FontDataForSizeAdvanced *p_data, int p_pixel_range, int p_rect_margin, FT_Outline *outline, const Vector2 &advance) const;
#endif
#ifdef MODULE_FREETYPE_ENABLED
_FORCE_INLINE_ FontGlyph rasterize_bitmap(FontDataForSizeAdvanced *p_data, int p_rect_margin, FT_Bitmap bitmap, int yofs, int xofs, const Vector2 &advance) const;
#endif
_FORCE_INLINE_ bool _ensure_glyph(FontDataAdvanced *p_font_data, const Vector2i &p_size, int32_t p_glyph) const;
_FORCE_INLINE_ bool _ensure_cache_for_size(FontDataAdvanced *p_font_data, const Vector2i &p_size) const;
_FORCE_INLINE_ void _font_clear_cache(FontDataAdvanced *p_font_data);
void _generateMTSDF_threaded(uint32_t y, void *p_td) const;
_FORCE_INLINE_ Vector2i _get_size(const FontDataAdvanced *p_font_data, int p_size) const {
if (p_font_data->msdf) {
return Vector2i(p_font_data->msdf_source_size, 0);
} else if (p_font_data->fixed_size > 0) {
return Vector2i(p_font_data->fixed_size, 0);
} else {
return Vector2i(p_size, 0);
}
}
_FORCE_INLINE_ Vector2i _get_size_outline(const FontDataAdvanced *p_font_data, const Vector2i &p_size) const {
if (p_font_data->msdf) {
return Vector2i(p_font_data->msdf_source_size, 0);
} else if (p_font_data->fixed_size > 0) {
return Vector2i(p_font_data->fixed_size, MIN(p_size.y, 1));
} else {
return p_size;
}
}
// Shaped text cache data.
struct ShapedTextDataAdvanced : public ShapedTextData {
/* Intermediate data */
Char16String utf16;
@ -88,7 +243,9 @@ class TextServerAdvanced : public TextServer {
}
};
float oversampling = 1.f;
// Common data.
real_t oversampling = 1.f;
mutable RID_PtrOwner<FontDataAdvanced> font_owner;
mutable RID_PtrOwner<ShapedTextDataAdvanced> shaped_owner;
@ -97,6 +254,30 @@ class TextServerAdvanced : public TextServer {
void _shape_run(ShapedTextDataAdvanced *p_sd, int32_t p_start, int32_t p_end, hb_script_t p_script, hb_direction_t p_direction, Vector<RID> p_fonts, int p_span, int p_fb_index);
TextServer::Glyph _shape_single_glyph(ShapedTextDataAdvanced *p_sd, char32_t p_char, hb_script_t p_script, hb_direction_t p_direction, RID p_font, int p_font_size);
// HarfBuzz bitmap font interface.
static hb_font_funcs_t *funcs;
struct hb_bmp_font_t {
TextServerAdvanced::FontDataForSizeAdvanced *face = nullptr;
bool unref = false; /* Whether to destroy bm_face when done. */
};
static hb_bmp_font_t *_hb_bmp_font_create(TextServerAdvanced::FontDataForSizeAdvanced *p_face, bool p_unref);
static void _hb_bmp_font_destroy(void *p_data);
static hb_bool_t hb_bmp_get_nominal_glyph(hb_font_t *p_font, void *p_font_data, hb_codepoint_t p_unicode, hb_codepoint_t *r_glyph, void *p_user_data);
static hb_position_t hb_bmp_get_glyph_h_advance(hb_font_t *p_font, void *p_font_data, hb_codepoint_t p_glyph, void *p_user_data);
static hb_position_t hb_bmp_get_glyph_v_advance(hb_font_t *p_font, void *p_font_data, hb_codepoint_t p_glyph, void *p_user_data);
static hb_position_t hb_bmp_get_glyph_h_kerning(hb_font_t *p_font, void *p_font_data, hb_codepoint_t p_left_glyph, hb_codepoint_t p_right_glyph, void *p_user_data);
static hb_position_t hb_bmp_get_glyph_v_kerning(hb_font_t *p_font, void *p_font_data, hb_codepoint_t p_left_glyph, hb_codepoint_t p_right_glyph, void *p_user_data);
static hb_bool_t hb_bmp_get_glyph_v_origin(hb_font_t *p_font, void *p_font_data, hb_codepoint_t p_glyph, hb_position_t *r_x, hb_position_t *r_y, void *p_user_data);
static hb_bool_t hb_bmp_get_glyph_extents(hb_font_t *p_font, void *p_font_data, hb_codepoint_t p_glyph, hb_glyph_extents_t *r_extents, void *p_user_data);
static hb_bool_t hb_bmp_get_font_h_extents(hb_font_t *p_font, void *p_font_data, hb_font_extents_t *r_metrics, void *p_user_data);
static void hb_bmp_create_font_funcs();
static void hb_bmp_free_font_funcs();
static void _hb_bmp_font_set_funcs(hb_font_t *p_font, TextServerAdvanced::FontDataForSizeAdvanced *p_face, bool p_unref);
static hb_font_t *hb_bmp_font_create(TextServerAdvanced::FontDataForSizeAdvanced *p_face, hb_destroy_func_t p_destroy);
protected:
static void _bind_methods(){};
@ -119,81 +300,132 @@ public:
virtual bool is_locale_right_to_left(const String &p_locale) override;
virtual int32_t name_to_tag(const String &p_name) override;
virtual String tag_to_name(int32_t p_tag) override;
virtual int32_t name_to_tag(const String &p_name) const override;
virtual String tag_to_name(int32_t p_tag) const override;
/* Font interface */
virtual RID create_font_system(const String &p_name, int p_base_size = 16) override;
virtual RID create_font_resource(const String &p_filename, int p_base_size = 16) override;
virtual RID create_font_memory(const uint8_t *p_data, size_t p_size, const String &p_type, int p_base_size = 16) override;
virtual RID create_font_bitmap(float p_height, float p_ascent, int p_base_size = 16) override;
virtual RID create_font() override;
virtual void font_bitmap_add_texture(RID p_font, const Ref<Texture> &p_texture) override;
virtual void font_bitmap_add_char(RID p_font, char32_t p_char, int p_texture_idx, const Rect2 &p_rect, const Size2 &p_align, float p_advance) override;
virtual void font_bitmap_add_kerning_pair(RID p_font, char32_t p_A, char32_t p_B, int p_kerning) override;
virtual void font_set_data(RID p_font_rid, const PackedByteArray &p_data) override;
virtual void font_set_data_ptr(RID p_font_rid, const uint8_t *p_data_ptr, size_t p_data_size) override;
virtual float font_get_height(RID p_font, int p_size) const override;
virtual float font_get_ascent(RID p_font, int p_size) const override;
virtual float font_get_descent(RID p_font, int p_size) const override;
virtual void font_set_antialiased(RID p_font_rid, bool p_antialiased) override;
virtual bool font_is_antialiased(RID p_font_rid) const override;
virtual float font_get_underline_position(RID p_font, int p_size) const override;
virtual float font_get_underline_thickness(RID p_font, int p_size) const override;
virtual void font_set_multichannel_signed_distance_field(RID p_font_rid, bool p_msdf) override;
virtual bool font_is_multichannel_signed_distance_field(RID p_font_rid) const override;
virtual int font_get_spacing_space(RID p_font) const override;
virtual void font_set_spacing_space(RID p_font, int p_value) override;
virtual void font_set_msdf_pixel_range(RID p_font_rid, int p_msdf_pixel_range) override;
virtual int font_get_msdf_pixel_range(RID p_font_rid) const override;
virtual int font_get_spacing_glyph(RID p_font) const override;
virtual void font_set_spacing_glyph(RID p_font, int p_value) override;
virtual void font_set_msdf_size(RID p_font_rid, int p_msdf_size) override;
virtual int font_get_msdf_size(RID p_font_rid) const override;
virtual void font_set_antialiased(RID p_font, bool p_antialiased) override;
virtual bool font_get_antialiased(RID p_font) const override;
virtual void font_set_fixed_size(RID p_font_rid, int p_fixed_size) override;
virtual int font_get_fixed_size(RID p_font_rid) const override;
virtual Dictionary font_get_feature_list(RID p_font) const override;
virtual Dictionary font_get_variation_list(RID p_font) const override;
virtual void font_set_force_autohinter(RID p_font_rid, bool p_force_autohinter) override;
virtual bool font_is_force_autohinter(RID p_font_rid) const override;
virtual void font_set_variation(RID p_font, const String &p_name, double p_value) override;
virtual double font_get_variation(RID p_font, const String &p_name) const override;
virtual void font_set_hinting(RID p_font_rid, TextServer::Hinting p_hinting) override;
virtual TextServer::Hinting font_get_hinting(RID p_font_rid) const override;
virtual void font_set_hinting(RID p_font, Hinting p_hinting) override;
virtual Hinting font_get_hinting(RID p_font) const override;
virtual void font_set_variation_coordinates(RID p_font_rid, const Dictionary &p_variation_coordinates) override;
virtual Dictionary font_get_variation_coordinates(RID p_font_rid) const override;
virtual void font_set_distance_field_hint(RID p_font, bool p_distance_field) override;
virtual bool font_get_distance_field_hint(RID p_font) const override;
virtual void font_set_oversampling(RID p_font_rid, real_t p_oversampling) override;
virtual real_t font_get_oversampling(RID p_font_rid) const override;
virtual void font_set_force_autohinter(RID p_font, bool p_enabeld) override;
virtual bool font_get_force_autohinter(RID p_font) const override;
virtual Array font_get_size_cache_list(RID p_font_rid) const override;
virtual void font_clear_size_cache(RID p_font_rid) override;
virtual void font_remove_size_cache(RID p_font_rid, const Vector2i &p_size) override;
virtual bool font_has_char(RID p_font, char32_t p_char) const override;
virtual String font_get_supported_chars(RID p_font) const override;
hb_font_t *_font_get_hb_handle(RID p_font, int p_font_size) const;
virtual bool font_has_outline(RID p_font) const override;
virtual float font_get_base_size(RID p_font) const override;
virtual void font_set_ascent(RID p_font_rid, int p_size, real_t p_ascent) override;
virtual real_t font_get_ascent(RID p_font_rid, int p_size) const override;
virtual bool font_is_language_supported(RID p_font, const String &p_language) const override;
virtual void font_set_language_support_override(RID p_font, const String &p_language, bool p_supported) override;
virtual bool font_get_language_support_override(RID p_font, const String &p_language) override;
virtual void font_remove_language_support_override(RID p_font, const String &p_language) override;
Vector<String> font_get_language_support_overrides(RID p_font) override;
virtual void font_set_descent(RID p_font_rid, int p_size, real_t p_descent) override;
virtual real_t font_get_descent(RID p_font_rid, int p_size) const override;
virtual bool font_is_script_supported(RID p_font, const String &p_script) const override;
virtual void font_set_script_support_override(RID p_font, const String &p_script, bool p_supported) override;
virtual bool font_get_script_support_override(RID p_font, const String &p_script) override;
virtual void font_remove_script_support_override(RID p_font, const String &p_script) override;
Vector<String> font_get_script_support_overrides(RID p_font) override;
virtual void font_set_underline_position(RID p_font_rid, int p_size, real_t p_underline_position) override;
virtual real_t font_get_underline_position(RID p_font_rid, int p_size) const override;
virtual uint32_t font_get_glyph_index(RID p_font, char32_t p_char, char32_t p_variation_selector = 0x0000) const override;
virtual Vector2 font_get_glyph_advance(RID p_font, uint32_t p_index, int p_size) const override;
virtual Vector2 font_get_glyph_kerning(RID p_font, uint32_t p_index_a, uint32_t p_index_b, int p_size) const override;
virtual void font_set_underline_thickness(RID p_font_rid, int p_size, real_t p_underline_thickness) override;
virtual real_t font_get_underline_thickness(RID p_font_rid, int p_size) const override;
virtual Vector2 font_draw_glyph(RID p_font, RID p_canvas, int p_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color = Color(1, 1, 1)) const override;
virtual Vector2 font_draw_glyph_outline(RID p_font, RID p_canvas, int p_size, int p_outline_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color = Color(1, 1, 1)) const override;
virtual void font_set_scale(RID p_font_rid, int p_size, real_t p_scale) override;
virtual real_t font_get_scale(RID p_font_rid, int p_size) const override;
virtual bool font_get_glyph_contours(RID p_font, int p_size, uint32_t p_index, Vector<Vector3> &r_points, Vector<int32_t> &r_contours, bool &r_orientation) const override;
virtual void font_set_spacing(RID p_font_rid, int p_size, SpacingType p_spacing, int p_value) override;
virtual int font_get_spacing(RID p_font_rid, int p_size, SpacingType p_spacing) const override;
virtual float font_get_oversampling() const override;
virtual void font_set_oversampling(float p_oversampling) override;
virtual int font_get_texture_count(RID p_font_rid, const Vector2i &p_size) const override;
virtual void font_clear_textures(RID p_font_rid, const Vector2i &p_size) override;
virtual void font_remove_texture(RID p_font_rid, const Vector2i &p_size, int p_texture_index) override;
virtual Vector<String> get_system_fonts() const override;
virtual void font_set_texture_image(RID p_font_rid, const Vector2i &p_size, int p_texture_index, const Ref<Image> &p_image) override;
virtual Ref<Image> font_get_texture_image(RID p_font_rid, const Vector2i &p_size, int p_texture_index) const override;
virtual void font_set_texture_offsets(RID p_font_rid, const Vector2i &p_size, int p_texture_index, const PackedInt32Array &p_offset) override;
virtual PackedInt32Array font_get_texture_offsets(RID p_font_rid, const Vector2i &p_size, int p_texture_index) const override;
virtual Array font_get_glyph_list(RID p_font_rid, const Vector2i &p_size) const override;
virtual void font_clear_glyphs(RID p_font_rid, const Vector2i &p_size) override;
virtual void font_remove_glyph(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph) override;
virtual Vector2 font_get_glyph_advance(RID p_font_rid, int p_size, int32_t p_glyph) const override;
virtual void font_set_glyph_advance(RID p_font_rid, int p_size, int32_t p_glyph, const Vector2 &p_advance) override;
virtual Vector2 font_get_glyph_offset(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph) const override;
virtual void font_set_glyph_offset(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph, const Vector2 &p_offset) override;
virtual Vector2 font_get_glyph_size(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph) const override;
virtual void font_set_glyph_size(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph, const Vector2 &p_gl_size) override;
virtual Rect2 font_get_glyph_uv_rect(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph) const override;
virtual void font_set_glyph_uv_rect(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph, const Rect2 &p_uv_rect) override;
virtual int font_get_glyph_texture_idx(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph) const override;
virtual void font_set_glyph_texture_idx(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph, int p_texture_idx) override;
virtual bool font_get_glyph_contours(RID p_font, int p_size, int32_t p_index, Vector<Vector3> &r_points, Vector<int32_t> &r_contours, bool &r_orientation) const override;
virtual Array font_get_kerning_list(RID p_font_rid, int p_size) const override;
virtual void font_clear_kerning_map(RID p_font_rid, int p_size) override;
virtual void font_remove_kerning(RID p_font_rid, int p_size, const Vector2i &p_glyph_pair) override;
virtual void font_set_kerning(RID p_font_rid, int p_size, const Vector2i &p_glyph_pair, const Vector2 &p_kerning) override;
virtual Vector2 font_get_kerning(RID p_font_rid, int p_size, const Vector2i &p_glyph_pair) const override;
virtual int32_t font_get_glyph_index(RID p_font_rid, int p_size, char32_t p_char, char32_t p_variation_selector = 0) const override;
virtual bool font_has_char(RID p_font_rid, char32_t p_char) const override;
virtual String font_get_supported_chars(RID p_font_rid) const override;
virtual void font_render_range(RID p_font, const Vector2i &p_size, char32_t p_start, char32_t p_end) override;
virtual void font_render_glyph(RID p_font_rid, const Vector2i &p_size, int32_t p_index) override;
virtual void font_draw_glyph(RID p_font, RID p_canvas, int p_size, const Vector2 &p_pos, int32_t p_index, const Color &p_color = Color(1, 1, 1)) const override;
virtual void font_draw_glyph_outline(RID p_font, RID p_canvas, int p_size, int p_outline_size, const Vector2 &p_pos, int32_t p_index, const Color &p_color = Color(1, 1, 1)) const override;
virtual bool font_is_language_supported(RID p_font_rid, const String &p_language) const override;
virtual void font_set_language_support_override(RID p_font_rid, const String &p_language, bool p_supported) override;
virtual bool font_get_language_support_override(RID p_font_rid, const String &p_language) override;
virtual void font_remove_language_support_override(RID p_font_rid, const String &p_language) override;
virtual Vector<String> font_get_language_support_overrides(RID p_font_rid) override;
virtual bool font_is_script_supported(RID p_font_rid, const String &p_script) const override;
virtual void font_set_script_support_override(RID p_font_rid, const String &p_script, bool p_supported) override;
virtual bool font_get_script_support_override(RID p_font_rid, const String &p_script) override;
virtual void font_remove_script_support_override(RID p_font_rid, const String &p_script) override;
virtual Vector<String> font_get_script_support_overrides(RID p_font_rid) override;
virtual Dictionary font_supported_feature_list(RID p_font_rid) const override;
virtual Dictionary font_supported_variation_list(RID p_font_rid) const override;
virtual real_t font_get_global_oversampling() const override;
virtual void font_set_global_oversampling(real_t p_oversampling) override;
/* Shaped text buffer interface */
@ -222,14 +454,14 @@ public:
virtual RID shaped_text_substr(RID p_shaped, int p_start, int p_length) const override;
virtual RID shaped_text_get_parent(RID p_shaped) const override;
virtual float shaped_text_fit_to_width(RID p_shaped, float p_width, uint8_t /*JustificationFlag*/ p_jst_flags = JUSTIFICATION_WORD_BOUND | JUSTIFICATION_KASHIDA) override;
virtual float shaped_text_tab_align(RID p_shaped, const Vector<float> &p_tab_stops) override;
virtual real_t shaped_text_fit_to_width(RID p_shaped, real_t p_width, uint8_t /*JustificationFlag*/ p_jst_flags = JUSTIFICATION_WORD_BOUND | JUSTIFICATION_KASHIDA) override;
virtual real_t shaped_text_tab_align(RID p_shaped, const Vector<real_t> &p_tab_stops) override;
virtual bool shaped_text_shape(RID p_shaped) override;
virtual bool shaped_text_update_breaks(RID p_shaped) override;
virtual bool shaped_text_update_justification_ops(RID p_shaped) override;
virtual void shaped_text_overrun_trim_to_width(RID p_shaped, float p_width, uint8_t p_trim_flags) override;
virtual void shaped_text_overrun_trim_to_width(RID p_shaped, real_t p_width, uint8_t p_trim_flags) override;
virtual TrimData shaped_text_get_trim_data(RID p_shaped) const override;
virtual bool shaped_text_is_ready(RID p_shaped) const override;
@ -244,11 +476,11 @@ public:
virtual Rect2 shaped_text_get_object_rect(RID p_shaped, Variant p_key) const override;
virtual Size2 shaped_text_get_size(RID p_shaped) const override;
virtual float shaped_text_get_ascent(RID p_shaped) const override;
virtual float shaped_text_get_descent(RID p_shaped) const override;
virtual float shaped_text_get_width(RID p_shaped) const override;
virtual float shaped_text_get_underline_position(RID p_shaped) const override;
virtual float shaped_text_get_underline_thickness(RID p_shaped) const override;
virtual real_t shaped_text_get_ascent(RID p_shaped) const override;
virtual real_t shaped_text_get_descent(RID p_shaped) const override;
virtual real_t shaped_text_get_width(RID p_shaped) const override;
virtual real_t shaped_text_get_underline_position(RID p_shaped) const override;
virtual real_t shaped_text_get_underline_thickness(RID p_shaped) const override;
virtual String format_number(const String &p_string, const String &p_language = "") const override;
virtual String parse_number(const String &p_string, const String &p_language = "") const override;

View File

@ -3,11 +3,23 @@
Import("env")
Import("env_modules")
freetype_enabled = env.module_check_dependencies("text_server_fb", ["freetype"], True)
msdngen_enabled = env.module_check_dependencies("text_server_fb", ["msdfgen"], True)
env_text_server_fb = env_modules.Clone()
env_text_server_fb.Append(
CPPPATH=[
"#thirdparty/freetype/include",
]
)
if msdngen_enabled:
env_text_server_fb.Append(
CPPPATH=[
"#thirdparty/msdfgen",
]
)
if freetype_enabled:
env_text_server_fb.Append(
CPPPATH=[
"#thirdparty/freetype/include",
]
)
env_text_server_fb.add_source_files(env.modules_sources, "*.cpp")

View File

@ -1,352 +0,0 @@
/*************************************************************************/
/* bitmap_font_fb.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* 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. */
/*************************************************************************/
#include "bitmap_font_fb.h"
Error BitmapFontDataFallback::load_from_file(const String &p_filename, int p_base_size) {
_THREAD_SAFE_METHOD_
//fnt format used by angelcode bmfont
//http://www.angelcode.com/products/bmfont/
FileAccess *f = FileAccess::open(p_filename, FileAccess::READ);
ERR_FAIL_COND_V_MSG(!f, ERR_FILE_NOT_FOUND, "Can't open font: " + p_filename + ".");
while (true) {
String line = f->get_line();
int delimiter = line.find(" ");
String type = line.substr(0, delimiter);
int pos = delimiter + 1;
Map<String, String> keys;
while (pos < line.size() && line[pos] == ' ') {
pos++;
}
while (pos < line.size()) {
int eq = line.find("=", pos);
if (eq == -1) {
break;
}
String key = line.substr(pos, eq - pos);
int end = -1;
String value;
if (line[eq + 1] == '"') {
end = line.find("\"", eq + 2);
if (end == -1) {
break;
}
value = line.substr(eq + 2, end - 1 - eq - 1);
pos = end + 1;
} else {
end = line.find(" ", eq + 1);
if (end == -1) {
end = line.size();
}
value = line.substr(eq + 1, end - eq);
pos = end;
}
while (pos < line.size() && line[pos] == ' ') {
pos++;
}
keys[key] = value;
}
if (type == "info") {
if (keys.has("size")) {
base_size = keys["size"].to_int();
}
} else if (type == "common") {
if (keys.has("lineHeight")) {
height = keys["lineHeight"].to_int();
}
if (keys.has("base")) {
ascent = keys["base"].to_int();
}
} else if (type == "page") {
if (keys.has("file")) {
String base_dir = p_filename.get_base_dir();
String file = base_dir.plus_file(keys["file"]);
if (RenderingServer::get_singleton() != nullptr) {
Ref<Texture2D> tex = ResourceLoader::load(file);
if (tex.is_null()) {
ERR_PRINT("Can't load font texture!");
} else {
ERR_FAIL_COND_V_MSG(tex.is_null(), ERR_FILE_CANT_READ, "It's not a reference to a valid Texture object.");
textures.push_back(tex);
}
}
}
} else if (type == "char") {
Character c;
char32_t idx = 0;
if (keys.has("id")) {
idx = keys["id"].to_int();
}
if (keys.has("x")) {
c.rect.position.x = keys["x"].to_int();
}
if (keys.has("y")) {
c.rect.position.y = keys["y"].to_int();
}
if (keys.has("width")) {
c.rect.size.width = keys["width"].to_int();
}
if (keys.has("height")) {
c.rect.size.height = keys["height"].to_int();
}
if (keys.has("xoffset")) {
c.align.x = keys["xoffset"].to_int();
}
if (keys.has("yoffset")) {
c.align.y = keys["yoffset"].to_int();
}
if (keys.has("page")) {
c.texture_idx = keys["page"].to_int();
}
if (keys.has("xadvance")) {
c.advance.x = keys["xadvance"].to_int();
}
if (keys.has("yadvance")) {
c.advance.y = keys["yadvance"].to_int();
}
if (c.advance.x < 0) {
c.advance.x = c.rect.size.width + 1;
}
if (c.advance.y < 0) {
c.advance.y = c.rect.size.height + 1;
}
char_map[idx] = c;
} else if (type == "kerning") {
KerningPairKey kpk;
float k = 0.0;
if (keys.has("first")) {
kpk.A = keys["first"].to_int();
}
if (keys.has("second")) {
kpk.B = keys["second"].to_int();
}
if (keys.has("amount")) {
k = keys["amount"].to_int();
}
kerning_map[kpk] = k;
}
if (f->eof_reached()) {
break;
}
}
if (base_size == 0) {
base_size = height;
}
valid = true;
memdelete(f);
return OK;
}
Error BitmapFontDataFallback::bitmap_new(float p_height, float p_ascent, int p_base_size) {
height = p_height;
ascent = p_ascent;
base_size = p_base_size;
if (base_size == 0) {
base_size = height;
}
char_map.clear();
textures.clear();
kerning_map.clear();
valid = true;
return OK;
}
void BitmapFontDataFallback::bitmap_add_texture(const Ref<Texture> &p_texture) {
ERR_FAIL_COND(!valid);
ERR_FAIL_COND_MSG(p_texture.is_null(), "It's not a reference to a valid Texture object.");
textures.push_back(p_texture);
}
void BitmapFontDataFallback::bitmap_add_char(char32_t p_char, int p_texture_idx, const Rect2 &p_rect, const Size2 &p_align, float p_advance) {
ERR_FAIL_COND(!valid);
Character chr;
chr.rect = p_rect;
chr.texture_idx = p_texture_idx;
if (p_advance < 0) {
chr.advance.x = chr.rect.size.x;
} else {
chr.advance.x = p_advance;
}
chr.align = p_align;
char_map[p_char] = chr;
}
void BitmapFontDataFallback::bitmap_add_kerning_pair(char32_t p_A, char32_t p_B, int p_kerning) {
ERR_FAIL_COND(!valid);
KerningPairKey kpk;
kpk.A = p_A;
kpk.B = p_B;
if (p_kerning == 0 && kerning_map.has(kpk)) {
kerning_map.erase(kpk);
} else {
kerning_map[kpk] = p_kerning;
}
}
float BitmapFontDataFallback::get_height(int p_size) const {
ERR_FAIL_COND_V(!valid, 0.f);
return height * (float(p_size) / float(base_size));
}
float BitmapFontDataFallback::get_ascent(int p_size) const {
ERR_FAIL_COND_V(!valid, 0.f);
return ascent * (float(p_size) / float(base_size));
}
float BitmapFontDataFallback::get_descent(int p_size) const {
ERR_FAIL_COND_V(!valid, 0.f);
return (height - ascent) * (float(p_size) / float(base_size));
}
float BitmapFontDataFallback::get_underline_position(int p_size) const {
ERR_FAIL_COND_V(!valid, 0.f);
return 2 * (float(p_size) / float(base_size));
}
float BitmapFontDataFallback::get_underline_thickness(int p_size) const {
ERR_FAIL_COND_V(!valid, 0.f);
return 1 * (float(p_size) / float(base_size));
}
void BitmapFontDataFallback::set_distance_field_hint(bool p_distance_field) {
distance_field_hint = p_distance_field;
}
bool BitmapFontDataFallback::get_distance_field_hint() const {
return distance_field_hint;
}
float BitmapFontDataFallback::get_base_size() const {
return base_size;
}
bool BitmapFontDataFallback::has_char(char32_t p_char) const {
_THREAD_SAFE_METHOD_
ERR_FAIL_COND_V(!valid, false);
return char_map.has(p_char);
}
String BitmapFontDataFallback::get_supported_chars() const {
_THREAD_SAFE_METHOD_
ERR_FAIL_COND_V(!valid, String());
String chars;
const char32_t *k = nullptr;
while ((k = char_map.next(k))) {
chars += char32_t(*k);
}
return chars;
}
Vector2 BitmapFontDataFallback::get_advance(char32_t p_char, int p_size) const {
_THREAD_SAFE_METHOD_
ERR_FAIL_COND_V(!valid, Vector2());
const Character *c = char_map.getptr(p_char);
ERR_FAIL_COND_V(c == nullptr, Vector2());
return c->advance * (float(p_size) / float(base_size));
}
Vector2 BitmapFontDataFallback::get_kerning(char32_t p_char, char32_t p_next, int p_size) const {
_THREAD_SAFE_METHOD_
ERR_FAIL_COND_V(!valid, Vector2());
KerningPairKey kpk;
kpk.A = p_char;
kpk.B = p_next;
const Map<KerningPairKey, int>::Element *E = kerning_map.find(kpk);
if (E) {
return Vector2(-E->get() * (float(p_size) / float(base_size)), 0);
} else {
return Vector2();
}
}
Vector2 BitmapFontDataFallback::draw_glyph(RID p_canvas, int p_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color) const {
_THREAD_SAFE_METHOD_
if (p_index == 0) {
return Vector2();
}
ERR_FAIL_COND_V(!valid, Vector2());
const Character *c = char_map.getptr(p_index);
ERR_FAIL_COND_V(c == nullptr, Vector2());
ERR_FAIL_COND_V(c->texture_idx < -1 || c->texture_idx >= textures.size(), Vector2());
if (c->texture_idx != -1) {
Point2i cpos = p_pos;
cpos += (c->align + Vector2(0, -ascent)) * (float(p_size) / float(base_size));
Size2i csize = c->rect.size * (float(p_size) / float(base_size));
if (RenderingServer::get_singleton() != nullptr) {
//if (distance_field_hint) { // Not implemented.
// RenderingServer::get_singleton()->canvas_item_set_distance_field_mode(p_canvas, true);
//}
RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, Rect2(cpos, csize), textures[c->texture_idx]->get_rid(), c->rect, p_color, false, false);
//if (distance_field_hint) {
// RenderingServer::get_singleton()->canvas_item_set_distance_field_mode(p_canvas, false);
//}
}
}
return c->advance * (float(p_size) / float(base_size));
}
Vector2 BitmapFontDataFallback::draw_glyph_outline(RID p_canvas, int p_size, int p_outline_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color) const {
_THREAD_SAFE_METHOD_
if (p_index == 0) {
return Vector2();
}
ERR_FAIL_COND_V(!valid, Vector2());
const Character *c = char_map.getptr(p_index);
ERR_FAIL_COND_V(c == nullptr, Vector2());
ERR_FAIL_COND_V(c->texture_idx < -1 || c->texture_idx >= textures.size(), Vector2());
// Not supported, return advance for compatibility.
return c->advance * (float(p_size) / float(base_size));
}

View File

@ -1,111 +0,0 @@
/*************************************************************************/
/* bitmap_font_fb.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* 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. */
/*************************************************************************/
#ifndef BITMAP_FONT_FALLBACK_H
#define BITMAP_FONT_FALLBACK_H
#include "font_fb.h"
struct BitmapFontDataFallback : public FontDataFallback {
_THREAD_SAFE_CLASS_
private:
Vector<Ref<Texture2D>> textures;
struct Character {
int texture_idx = 0;
Rect2 rect;
Vector2 align;
Vector2 advance = Vector2(-1, -1);
};
struct KerningPairKey {
union {
struct {
uint32_t A, B;
};
uint64_t pair = 0;
};
_FORCE_INLINE_ bool operator<(const KerningPairKey &p_r) const { return pair < p_r.pair; }
};
HashMap<char32_t, Character> char_map;
Map<KerningPairKey, int> kerning_map;
float height = 0.f;
float ascent = 0.f;
int base_size = 0;
bool distance_field_hint = false;
public:
virtual void clear_cache() override{};
virtual Error load_from_file(const String &p_filename, int p_base_size) override;
virtual Error bitmap_new(float p_height, float p_ascent, int p_base_size) override;
virtual void bitmap_add_texture(const Ref<Texture> &p_texture) override;
virtual void bitmap_add_char(char32_t p_char, int p_texture_idx, const Rect2 &p_rect, const Size2 &p_align, float p_advance) override;
virtual void bitmap_add_kerning_pair(char32_t p_A, char32_t p_B, int p_kerning) override;
virtual float get_height(int p_size) const override;
virtual float get_ascent(int p_size) const override;
virtual float get_descent(int p_size) const override;
virtual float get_underline_position(int p_size) const override;
virtual float get_underline_thickness(int p_size) const override;
virtual void set_antialiased(bool p_antialiased) override{};
virtual bool get_antialiased() const override { return false; };
virtual void set_hinting(TextServer::Hinting p_hinting) override{};
virtual TextServer::Hinting get_hinting() const override { return TextServer::HINTING_NONE; };
virtual void set_distance_field_hint(bool p_distance_field) override;
virtual bool get_distance_field_hint() const override;
virtual void set_force_autohinter(bool p_enabeld) override{};
virtual bool get_force_autohinter() const override { return false; };
virtual bool has_outline() const override { return false; };
virtual float get_base_size() const override;
virtual bool has_char(char32_t p_char) const override;
virtual String get_supported_chars() const override;
virtual Vector2 get_advance(char32_t p_char, int p_size) const override;
virtual Vector2 get_kerning(char32_t p_char, char32_t p_next, int p_size) const override;
virtual Vector2 draw_glyph(RID p_canvas, int p_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color) const override;
virtual Vector2 draw_glyph_outline(RID p_canvas, int p_size, int p_outline_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color) const override;
};
#endif // BITMAP_FONT_FALLBACK_H

View File

@ -1,713 +0,0 @@
/*************************************************************************/
/* dynamic_font_fb.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* 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. */
/*************************************************************************/
#include "dynamic_font_fb.h"
#ifdef MODULE_FREETYPE_ENABLED
#include FT_STROKER_H
#include FT_ADVANCES_H
DynamicFontDataFallback::DataAtSize *DynamicFontDataFallback::get_data_for_size(int p_size, int p_outline_size) {
ERR_FAIL_COND_V(!valid, nullptr);
ERR_FAIL_COND_V(p_size < 0 || p_size > UINT16_MAX, nullptr);
ERR_FAIL_COND_V(p_outline_size < 0 || p_outline_size > UINT16_MAX, nullptr);
CacheID id;
id.size = p_size;
id.outline_size = p_outline_size;
DataAtSize *fds = nullptr;
Map<CacheID, DataAtSize *>::Element *E = nullptr;
if (p_outline_size != 0) {
E = size_cache_outline.find(id);
} else {
E = size_cache.find(id);
}
if (E != nullptr) {
fds = E->get();
} else {
if (font_mem == nullptr && font_path != String()) {
if (!font_mem_cache.is_empty()) {
font_mem = font_mem_cache.ptr();
font_mem_size = font_mem_cache.size();
} else {
FileAccess *f = FileAccess::open(font_path, FileAccess::READ);
if (!f) {
ERR_FAIL_V_MSG(nullptr, "Cannot open font file '" + font_path + "'.");
}
uint64_t len = f->get_length();
font_mem_cache.resize(len);
f->get_buffer(font_mem_cache.ptrw(), len);
font_mem = font_mem_cache.ptr();
font_mem_size = len;
f->close();
}
}
int error = 0;
fds = memnew(DataAtSize);
if (font_mem) {
memset(&fds->stream, 0, sizeof(FT_StreamRec));
fds->stream.base = (unsigned char *)font_mem;
fds->stream.size = font_mem_size;
fds->stream.pos = 0;
FT_Open_Args fargs;
memset(&fargs, 0, sizeof(FT_Open_Args));
fargs.memory_base = (unsigned char *)font_mem;
fargs.memory_size = font_mem_size;
fargs.flags = FT_OPEN_MEMORY;
fargs.stream = &fds->stream;
error = FT_Open_Face(library, &fargs, 0, &fds->face);
} else {
memdelete(fds);
ERR_FAIL_V_MSG(nullptr, "DynamicFont uninitialized.");
}
if (error == FT_Err_Unknown_File_Format) {
memdelete(fds);
ERR_FAIL_V_MSG(nullptr, "Unknown font format.");
} else if (error) {
memdelete(fds);
ERR_FAIL_V_MSG(nullptr, "Error loading font.");
}
oversampling = TS->font_get_oversampling();
if (FT_HAS_COLOR(fds->face) && fds->face->num_fixed_sizes > 0) {
int best_match = 0;
int diff = ABS(p_size - ((int64_t)fds->face->available_sizes[0].width));
fds->scale_color_font = float(p_size * oversampling) / fds->face->available_sizes[0].width;
for (int i = 1; i < fds->face->num_fixed_sizes; i++) {
int ndiff = ABS(p_size - ((int64_t)fds->face->available_sizes[i].width));
if (ndiff < diff) {
best_match = i;
diff = ndiff;
fds->scale_color_font = float(p_size * oversampling) / fds->face->available_sizes[i].width;
}
}
FT_Select_Size(fds->face, best_match);
} else {
FT_Set_Pixel_Sizes(fds->face, 0, p_size * oversampling);
}
fds->size = p_size;
fds->ascent = (fds->face->size->metrics.ascender / 64.0) / oversampling * fds->scale_color_font;
fds->descent = (-fds->face->size->metrics.descender / 64.0) / oversampling * fds->scale_color_font;
fds->underline_position = (-FT_MulFix(fds->face->underline_position, fds->face->size->metrics.y_scale) / 64.0) / oversampling * fds->scale_color_font;
fds->underline_thickness = (FT_MulFix(fds->face->underline_thickness, fds->face->size->metrics.y_scale) / 64.0) / oversampling * fds->scale_color_font;
if (p_outline_size != 0) {
size_cache_outline[id] = fds;
} else {
size_cache[id] = fds;
}
}
return fds;
}
DynamicFontDataFallback::TexturePosition DynamicFontDataFallback::find_texture_pos_for_glyph(DynamicFontDataFallback::DataAtSize *p_data, int p_color_size, Image::Format p_image_format, int p_width, int p_height) {
TexturePosition ret;
ret.index = -1;
int mw = p_width;
int mh = p_height;
for (int i = 0; i < p_data->textures.size(); i++) {
const CharTexture &ct = p_data->textures[i];
if (RenderingServer::get_singleton() != nullptr) {
if (ct.texture->get_format() != p_image_format) {
continue;
}
}
if (mw > ct.texture_size || mh > ct.texture_size) { //too big for this texture
continue;
}
ret.y = 0x7FFFFFFF;
ret.x = 0;
for (int j = 0; j < ct.texture_size - mw; j++) {
int max_y = 0;
for (int k = j; k < j + mw; k++) {
int y = ct.offsets[k];
if (y > max_y) {
max_y = y;
}
}
if (max_y < ret.y) {
ret.y = max_y;
ret.x = j;
}
}
if (ret.y == 0x7FFFFFFF || ret.y + mh > ct.texture_size) {
continue; //fail, could not fit it here
}
ret.index = i;
break;
}
if (ret.index == -1) {
//could not find texture to fit, create one
ret.x = 0;
ret.y = 0;
int texsize = MAX(p_data->size * oversampling * 8, 256);
if (mw > texsize) {
texsize = mw; //special case, adapt to it?
}
if (mh > texsize) {
texsize = mh; //special case, adapt to it?
}
texsize = next_power_of_2(texsize);
texsize = MIN(texsize, 4096);
CharTexture tex;
tex.texture_size = texsize;
tex.imgdata.resize(texsize * texsize * p_color_size); //grayscale alpha
{
//zero texture
uint8_t *w = tex.imgdata.ptrw();
ERR_FAIL_COND_V(texsize * texsize * p_color_size > tex.imgdata.size(), ret);
// Initialize the texture to all-white pixels to prevent artifacts when the
// font is displayed at a non-default scale with filtering enabled.
if (p_color_size == 2) {
for (int i = 0; i < texsize * texsize * p_color_size; i += 2) { // FORMAT_LA8
w[i + 0] = 255;
w[i + 1] = 0;
}
} else if (p_color_size == 4) {
for (int i = 0; i < texsize * texsize * p_color_size; i += 4) { // FORMAT_RGBA8
w[i + 0] = 255;
w[i + 1] = 255;
w[i + 2] = 255;
w[i + 3] = 0;
}
} else {
ERR_FAIL_V(ret);
}
}
tex.offsets.resize(texsize);
for (int i = 0; i < texsize; i++) { //zero offsets
tex.offsets.write[i] = 0;
}
p_data->textures.push_back(tex);
ret.index = p_data->textures.size() - 1;
}
return ret;
}
DynamicFontDataFallback::Character DynamicFontDataFallback::Character::not_found() {
Character ch;
return ch;
}
DynamicFontDataFallback::Character DynamicFontDataFallback::bitmap_to_character(DynamicFontDataFallback::DataAtSize *p_data, FT_Bitmap bitmap, int yofs, int xofs, const Vector2 &advance) {
int w = bitmap.width;
int h = bitmap.rows;
int mw = w + rect_margin * 2;
int mh = h + rect_margin * 2;
ERR_FAIL_COND_V(mw > 4096, Character::not_found());
ERR_FAIL_COND_V(mh > 4096, Character::not_found());
int color_size = bitmap.pixel_mode == FT_PIXEL_MODE_BGRA ? 4 : 2;
Image::Format require_format = color_size == 4 ? Image::FORMAT_RGBA8 : Image::FORMAT_LA8;
TexturePosition tex_pos = find_texture_pos_for_glyph(p_data, color_size, require_format, mw, mh);
ERR_FAIL_COND_V(tex_pos.index < 0, Character::not_found());
//fit character in char texture
CharTexture &tex = p_data->textures.write[tex_pos.index];
{
uint8_t *wr = tex.imgdata.ptrw();
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
int ofs = ((i + tex_pos.y + rect_margin) * tex.texture_size + j + tex_pos.x + rect_margin) * color_size;
ERR_FAIL_COND_V(ofs >= tex.imgdata.size(), Character::not_found());
switch (bitmap.pixel_mode) {
case FT_PIXEL_MODE_MONO: {
int byte = i * bitmap.pitch + (j >> 3);
int bit = 1 << (7 - (j % 8));
wr[ofs + 0] = 255; //grayscale as 1
wr[ofs + 1] = (bitmap.buffer[byte] & bit) ? 255 : 0;
} break;
case FT_PIXEL_MODE_GRAY:
wr[ofs + 0] = 255; //grayscale as 1
wr[ofs + 1] = bitmap.buffer[i * bitmap.pitch + j];
break;
case FT_PIXEL_MODE_BGRA: {
int ofs_color = i * bitmap.pitch + (j << 2);
wr[ofs + 2] = bitmap.buffer[ofs_color + 0];
wr[ofs + 1] = bitmap.buffer[ofs_color + 1];
wr[ofs + 0] = bitmap.buffer[ofs_color + 2];
wr[ofs + 3] = bitmap.buffer[ofs_color + 3];
} break;
// TODO: FT_PIXEL_MODE_LCD
default:
ERR_FAIL_V_MSG(Character::not_found(), "Font uses unsupported pixel format: " + itos(bitmap.pixel_mode) + ".");
break;
}
}
}
}
//blit to image and texture
{
if (RenderingServer::get_singleton() != nullptr) {
Ref<Image> img = memnew(Image(tex.texture_size, tex.texture_size, 0, require_format, tex.imgdata));
if (tex.texture.is_null()) {
tex.texture.instantiate();
tex.texture->create_from_image(img);
} else {
tex.texture->update(img); //update
}
}
}
// update height array
for (int k = tex_pos.x; k < tex_pos.x + mw; k++) {
tex.offsets.write[k] = tex_pos.y + mh;
}
Character chr;
chr.align = (Vector2(xofs, -yofs) * p_data->scale_color_font / oversampling).round();
chr.advance = (advance * p_data->scale_color_font / oversampling).round();
chr.texture_idx = tex_pos.index;
chr.found = true;
chr.rect_uv = Rect2(tex_pos.x + rect_margin, tex_pos.y + rect_margin, w, h);
chr.rect = chr.rect_uv;
chr.rect.position /= oversampling;
chr.rect.size *= (p_data->scale_color_font / oversampling);
return chr;
}
void DynamicFontDataFallback::update_char(int p_size, char32_t p_char) {
DataAtSize *fds = get_data_for_size(p_size, false);
ERR_FAIL_COND(fds == nullptr);
if (fds->char_map.has(p_char)) {
return;
}
Character character = Character::not_found();
FT_GlyphSlot slot = fds->face->glyph;
FT_UInt gl_index = FT_Get_Char_Index(fds->face, p_char);
if (gl_index == 0) {
fds->char_map[p_char] = character;
return;
}
int ft_hinting;
switch (hinting) {
case TextServer::HINTING_NONE:
ft_hinting = FT_LOAD_NO_HINTING;
break;
case TextServer::HINTING_LIGHT:
ft_hinting = FT_LOAD_TARGET_LIGHT;
break;
default:
ft_hinting = FT_LOAD_TARGET_NORMAL;
break;
}
FT_Fixed v, h;
FT_Get_Advance(fds->face, gl_index, FT_HAS_COLOR(fds->face) ? FT_LOAD_COLOR : FT_LOAD_DEFAULT | (force_autohinter ? FT_LOAD_FORCE_AUTOHINT : 0) | ft_hinting, &h);
FT_Get_Advance(fds->face, gl_index, FT_HAS_COLOR(fds->face) ? FT_LOAD_COLOR : FT_LOAD_DEFAULT | (force_autohinter ? FT_LOAD_FORCE_AUTOHINT : 0) | ft_hinting | FT_LOAD_VERTICAL_LAYOUT, &v);
int error = FT_Load_Glyph(fds->face, gl_index, FT_HAS_COLOR(fds->face) ? FT_LOAD_COLOR : FT_LOAD_DEFAULT | (force_autohinter ? FT_LOAD_FORCE_AUTOHINT : 0) | ft_hinting);
if (error) {
fds->char_map[p_char] = character;
return;
}
error = FT_Render_Glyph(fds->face->glyph, antialiased ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO);
if (!error) {
character = bitmap_to_character(fds, slot->bitmap, slot->bitmap_top, slot->bitmap_left, Vector2((h + (1 << 9)) >> 10, (v + (1 << 9)) >> 10) / 64.0);
}
fds->char_map[p_char] = character;
}
void DynamicFontDataFallback::update_char_outline(int p_size, int p_outline_size, char32_t p_char) {
DataAtSize *fds = get_data_for_size(p_size, p_outline_size);
ERR_FAIL_COND(fds == nullptr);
if (fds->char_map.has(p_char)) {
return;
}
Character character = Character::not_found();
FT_UInt gl_index = FT_Get_Char_Index(fds->face, p_char);
if (gl_index == 0) {
fds->char_map[p_char] = character;
return;
}
int error = FT_Load_Glyph(fds->face, gl_index, FT_LOAD_NO_BITMAP | (force_autohinter ? FT_LOAD_FORCE_AUTOHINT : 0));
if (error) {
fds->char_map[p_char] = character;
return;
}
FT_Stroker stroker;
if (FT_Stroker_New(library, &stroker) != 0) {
fds->char_map[p_char] = character;
return;
}
FT_Stroker_Set(stroker, (int)(p_outline_size * oversampling * 64.0), FT_STROKER_LINECAP_BUTT, FT_STROKER_LINEJOIN_ROUND, 0);
FT_Glyph glyph;
FT_BitmapGlyph glyph_bitmap;
if (FT_Get_Glyph(fds->face->glyph, &glyph) != 0) {
goto cleanup_stroker;
}
if (FT_Glyph_Stroke(&glyph, stroker, 1) != 0) {
goto cleanup_glyph;
}
if (FT_Glyph_To_Bitmap(&glyph, antialiased ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO, nullptr, 1) != 0) {
goto cleanup_glyph;
}
glyph_bitmap = (FT_BitmapGlyph)glyph;
character = bitmap_to_character(fds, glyph_bitmap->bitmap, glyph_bitmap->top, glyph_bitmap->left, Vector2());
cleanup_glyph:
FT_Done_Glyph(glyph);
cleanup_stroker:
FT_Stroker_Done(stroker);
fds->char_map[p_char] = character;
}
void DynamicFontDataFallback::clear_cache() {
_THREAD_SAFE_METHOD_
for (Map<CacheID, DataAtSize *>::Element *E = size_cache.front(); E; E = E->next()) {
memdelete(E->get());
}
size_cache.clear();
for (Map<CacheID, DataAtSize *>::Element *E = size_cache_outline.front(); E; E = E->next()) {
memdelete(E->get());
}
size_cache_outline.clear();
}
Error DynamicFontDataFallback::load_from_file(const String &p_filename, int p_base_size) {
_THREAD_SAFE_METHOD_
if (library == nullptr) {
int error = FT_Init_FreeType(&library);
ERR_FAIL_COND_V_MSG(error != 0, ERR_CANT_CREATE, "Error initializing FreeType.");
}
clear_cache();
font_path = p_filename;
base_size = p_base_size;
valid = true;
DataAtSize *fds = get_data_for_size(base_size); // load base size.
if (fds == nullptr) {
valid = false;
ERR_FAIL_V(ERR_CANT_CREATE);
}
return OK;
}
Error DynamicFontDataFallback::load_from_memory(const uint8_t *p_data, size_t p_size, int p_base_size) {
_THREAD_SAFE_METHOD_
if (library == nullptr) {
int error = FT_Init_FreeType(&library);
ERR_FAIL_COND_V_MSG(error != 0, ERR_CANT_CREATE, "Error initializing FreeType.");
}
clear_cache();
font_mem = p_data;
font_mem_size = p_size;
base_size = p_base_size;
valid = true;
DataAtSize *fds = get_data_for_size(base_size); // load base size.
if (fds == nullptr) {
valid = false;
ERR_FAIL_V(ERR_CANT_CREATE);
}
return OK;
}
float DynamicFontDataFallback::get_height(int p_size) const {
_THREAD_SAFE_METHOD_
DataAtSize *fds = const_cast<DynamicFontDataFallback *>(this)->get_data_for_size(p_size);
ERR_FAIL_COND_V(fds == nullptr, 0.f);
return fds->ascent + fds->descent;
}
float DynamicFontDataFallback::get_ascent(int p_size) const {
_THREAD_SAFE_METHOD_
DataAtSize *fds = const_cast<DynamicFontDataFallback *>(this)->get_data_for_size(p_size);
ERR_FAIL_COND_V(fds == nullptr, 0.f);
return fds->ascent;
}
float DynamicFontDataFallback::get_descent(int p_size) const {
_THREAD_SAFE_METHOD_
DataAtSize *fds = const_cast<DynamicFontDataFallback *>(this)->get_data_for_size(p_size);
ERR_FAIL_COND_V(fds == nullptr, 0.f);
return fds->descent;
}
float DynamicFontDataFallback::get_underline_position(int p_size) const {
_THREAD_SAFE_METHOD_
DataAtSize *fds = const_cast<DynamicFontDataFallback *>(this)->get_data_for_size(p_size);
ERR_FAIL_COND_V(fds == nullptr, 0.f);
return fds->underline_position;
}
float DynamicFontDataFallback::get_underline_thickness(int p_size) const {
_THREAD_SAFE_METHOD_
DataAtSize *fds = const_cast<DynamicFontDataFallback *>(this)->get_data_for_size(p_size);
ERR_FAIL_COND_V(fds == nullptr, 0.f);
return fds->underline_thickness;
}
void DynamicFontDataFallback::set_antialiased(bool p_antialiased) {
if (antialiased != p_antialiased) {
clear_cache();
antialiased = p_antialiased;
}
}
bool DynamicFontDataFallback::get_antialiased() const {
return antialiased;
}
void DynamicFontDataFallback::set_force_autohinter(bool p_enabled) {
if (force_autohinter != p_enabled) {
clear_cache();
force_autohinter = p_enabled;
}
}
bool DynamicFontDataFallback::get_force_autohinter() const {
return force_autohinter;
}
void DynamicFontDataFallback::set_hinting(TextServer::Hinting p_hinting) {
if (hinting != p_hinting) {
clear_cache();
hinting = p_hinting;
}
}
TextServer::Hinting DynamicFontDataFallback::get_hinting() const {
return hinting;
}
bool DynamicFontDataFallback::has_outline() const {
return true;
}
float DynamicFontDataFallback::get_base_size() const {
return base_size;
}
bool DynamicFontDataFallback::has_char(char32_t p_char) const {
_THREAD_SAFE_METHOD_
DataAtSize *fds = const_cast<DynamicFontDataFallback *>(this)->get_data_for_size(base_size);
ERR_FAIL_COND_V(fds == nullptr, false);
const_cast<DynamicFontDataFallback *>(this)->update_char(base_size, p_char);
Character ch = fds->char_map[p_char];
return (ch.found);
}
String DynamicFontDataFallback::get_supported_chars() const {
_THREAD_SAFE_METHOD_
DataAtSize *fds = const_cast<DynamicFontDataFallback *>(this)->get_data_for_size(base_size);
ERR_FAIL_COND_V(fds == nullptr, String());
String chars;
FT_UInt gindex;
FT_ULong charcode = FT_Get_First_Char(fds->face, &gindex);
while (gindex != 0) {
if (charcode != 0) {
chars += char32_t(charcode);
}
charcode = FT_Get_Next_Char(fds->face, charcode, &gindex);
}
return chars;
}
Vector2 DynamicFontDataFallback::get_advance(char32_t p_char, int p_size) const {
_THREAD_SAFE_METHOD_
DataAtSize *fds = const_cast<DynamicFontDataFallback *>(this)->get_data_for_size(p_size);
ERR_FAIL_COND_V(fds == nullptr, Vector2());
const_cast<DynamicFontDataFallback *>(this)->update_char(p_size, p_char);
Character ch = fds->char_map[p_char];
return ch.advance;
}
Vector2 DynamicFontDataFallback::get_kerning(char32_t p_char, char32_t p_next, int p_size) const {
_THREAD_SAFE_METHOD_
DataAtSize *fds = const_cast<DynamicFontDataFallback *>(this)->get_data_for_size(p_size);
ERR_FAIL_COND_V(fds == nullptr, Vector2());
FT_Vector delta;
FT_Get_Kerning(fds->face, FT_Get_Char_Index(fds->face, p_char), FT_Get_Char_Index(fds->face, p_next), FT_KERNING_DEFAULT, &delta);
return Vector2(delta.x, delta.y);
}
Vector2 DynamicFontDataFallback::draw_glyph(RID p_canvas, int p_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color) const {
_THREAD_SAFE_METHOD_
DataAtSize *fds = const_cast<DynamicFontDataFallback *>(this)->get_data_for_size(p_size);
ERR_FAIL_COND_V(fds == nullptr, Vector2());
const_cast<DynamicFontDataFallback *>(this)->update_char(p_size, p_index);
Character ch = fds->char_map[p_index];
Vector2 advance;
if (ch.found) {
ERR_FAIL_COND_V(ch.texture_idx < -1 || ch.texture_idx >= fds->textures.size(), Vector2());
if (ch.texture_idx != -1) {
Point2i cpos = p_pos;
cpos += ch.align;
Color modulate = p_color;
if (FT_HAS_COLOR(fds->face)) {
modulate.r = modulate.g = modulate.b = 1.0;
}
if (RenderingServer::get_singleton() != nullptr) {
RID texture = fds->textures[ch.texture_idx].texture->get_rid();
RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, Rect2(cpos, ch.rect.size), texture, ch.rect_uv, modulate, false, false);
}
}
advance = ch.advance;
}
return advance;
}
Vector2 DynamicFontDataFallback::draw_glyph_outline(RID p_canvas, int p_size, int p_outline_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color) const {
_THREAD_SAFE_METHOD_
DataAtSize *fds = const_cast<DynamicFontDataFallback *>(this)->get_data_for_size(p_size, p_outline_size);
ERR_FAIL_COND_V(fds == nullptr, Vector2());
const_cast<DynamicFontDataFallback *>(this)->update_char_outline(p_size, p_outline_size, p_index);
Character ch = fds->char_map[p_index];
Vector2 advance;
if (ch.found) {
ERR_FAIL_COND_V(ch.texture_idx < -1 || ch.texture_idx >= fds->textures.size(), Vector2());
if (ch.texture_idx != -1) {
Point2i cpos = p_pos;
cpos += ch.align;
Color modulate = p_color;
if (FT_HAS_COLOR(fds->face)) {
modulate.r = modulate.g = modulate.b = 1.0;
}
if (RenderingServer::get_singleton() != nullptr) {
RID texture = fds->textures[ch.texture_idx].texture->get_rid();
RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, Rect2(cpos, ch.rect.size), texture, ch.rect_uv, modulate, false, false);
}
}
advance = ch.advance;
}
return advance;
}
bool DynamicFontDataFallback::get_glyph_contours(int p_size, uint32_t p_index, Vector<Vector3> &r_points, Vector<int32_t> &r_contours, bool &r_orientation) const {
_THREAD_SAFE_METHOD_
DataAtSize *fds = const_cast<DynamicFontDataFallback *>(this)->get_data_for_size(p_size);
ERR_FAIL_COND_V(fds == nullptr, false);
int error = FT_Load_Glyph(fds->face, p_index, FT_LOAD_NO_BITMAP | (force_autohinter ? FT_LOAD_FORCE_AUTOHINT : 0));
ERR_FAIL_COND_V(error, false);
r_points.clear();
r_contours.clear();
float h = fds->ascent;
float scale = (1.0 / 64.0) / oversampling * fds->scale_color_font;
for (short i = 0; i < fds->face->glyph->outline.n_points; i++) {
r_points.push_back(Vector3(fds->face->glyph->outline.points[i].x * scale, h - fds->face->glyph->outline.points[i].y * scale, FT_CURVE_TAG(fds->face->glyph->outline.tags[i])));
}
for (short i = 0; i < fds->face->glyph->outline.n_contours; i++) {
r_contours.push_back(fds->face->glyph->outline.contours[i]);
}
r_orientation = (FT_Outline_Get_Orientation(&fds->face->glyph->outline) == FT_ORIENTATION_FILL_RIGHT);
return true;
}
DynamicFontDataFallback::~DynamicFontDataFallback() {
clear_cache();
if (library != nullptr) {
FT_Done_FreeType(library);
}
}
#endif // MODULE_FREETYPE_ENABLED

View File

@ -1,173 +0,0 @@
/*************************************************************************/
/* dynamic_font_fb.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* 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. */
/*************************************************************************/
#ifndef DYNAMIC_FONT_FALLBACK_H
#define DYNAMIC_FONT_FALLBACK_H
#include "font_fb.h"
#include "modules/modules_enabled.gen.h"
#ifdef MODULE_FREETYPE_ENABLED
#include <ft2build.h>
#include FT_FREETYPE_H
struct DynamicFontDataFallback : public FontDataFallback {
_THREAD_SAFE_CLASS_
private:
struct CharTexture {
Vector<uint8_t> imgdata;
int texture_size = 0;
Vector<int> offsets;
Ref<ImageTexture> texture;
};
struct Character {
bool found = false;
int texture_idx = 0;
Rect2 rect;
Rect2 rect_uv;
Vector2 align;
Vector2 advance = Vector2(-1, -1);
static Character not_found();
};
struct TexturePosition {
int index = 0;
int x = 0;
int y = 0;
};
struct CacheID {
union {
struct {
uint32_t size : 16;
uint32_t outline_size : 16;
};
uint32_t key = 0;
};
bool operator<(CacheID right) const {
return key < right.key;
}
};
struct DataAtSize {
FT_Face face = nullptr;
FT_StreamRec stream;
int size = 0;
float scale_color_font = 1.f;
float ascent = 0.0;
float descent = 0.0;
float underline_position = 0.0;
float underline_thickness = 0.0;
Vector<CharTexture> textures;
HashMap<char32_t, Character> char_map;
~DataAtSize() {
if (face != nullptr) {
FT_Done_Face(face);
}
}
};
FT_Library library = nullptr;
// Source data.
const uint8_t *font_mem = nullptr;
int font_mem_size = 0;
String font_path;
Vector<uint8_t> font_mem_cache;
float rect_margin = 1.f;
int base_size = 16;
float oversampling = 1.f;
bool antialiased = true;
bool force_autohinter = false;
TextServer::Hinting hinting = TextServer::HINTING_LIGHT;
Map<CacheID, DataAtSize *> size_cache;
Map<CacheID, DataAtSize *> size_cache_outline;
DataAtSize *get_data_for_size(int p_size, int p_outline_size = 0);
TexturePosition find_texture_pos_for_glyph(DataAtSize *p_data, int p_color_size, Image::Format p_image_format, int p_width, int p_height);
Character bitmap_to_character(DataAtSize *p_data, FT_Bitmap bitmap, int yofs, int xofs, const Vector2 &advance);
_FORCE_INLINE_ void update_char(int p_size, char32_t p_char);
_FORCE_INLINE_ void update_char_outline(int p_size, int p_outline_size, char32_t p_char);
public:
virtual void clear_cache() override;
virtual Error load_from_file(const String &p_filename, int p_base_size) override;
virtual Error load_from_memory(const uint8_t *p_data, size_t p_size, int p_base_size) override;
virtual float get_height(int p_size) const override;
virtual float get_ascent(int p_size) const override;
virtual float get_descent(int p_size) const override;
virtual float get_underline_position(int p_size) const override;
virtual float get_underline_thickness(int p_size) const override;
virtual void set_antialiased(bool p_antialiased) override;
virtual bool get_antialiased() const override;
virtual void set_hinting(TextServer::Hinting p_hinting) override;
virtual TextServer::Hinting get_hinting() const override;
virtual void set_force_autohinter(bool p_enabeld) override;
virtual bool get_force_autohinter() const override;
virtual void set_distance_field_hint(bool p_distance_field) override{};
virtual bool get_distance_field_hint() const override { return false; };
virtual bool has_outline() const override;
virtual float get_base_size() const override;
virtual bool has_char(char32_t p_char) const override;
virtual String get_supported_chars() const override;
virtual Vector2 get_advance(char32_t p_char, int p_size) const override;
virtual Vector2 get_kerning(char32_t p_char, char32_t p_next, int p_size) const override;
virtual Vector2 draw_glyph(RID p_canvas, int p_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color) const override;
virtual Vector2 draw_glyph_outline(RID p_canvas, int p_size, int p_outline_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color) const override;
virtual bool get_glyph_contours(int p_size, uint32_t p_index, Vector<Vector3> &r_points, Vector<int32_t> &r_contours, bool &r_orientation) const override;
virtual ~DynamicFontDataFallback() override;
};
#endif // MODULE_FREETYPE_ENABLED
#endif // DYNAMIC_FONT_FALLBACK_H

View File

@ -1,101 +0,0 @@
/*************************************************************************/
/* font_fb.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* 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. */
/*************************************************************************/
#ifndef FONT_FALLBACK_H
#define FONT_FALLBACK_H
#include "servers/text_server.h"
struct FontDataFallback {
Map<String, bool> lang_support_overrides;
Map<String, bool> script_support_overrides;
bool valid = false;
int spacing_space = 0;
int spacing_glyph = 0;
virtual void clear_cache() = 0;
virtual Error load_from_file(const String &p_filename, int p_base_size) { return ERR_CANT_CREATE; };
virtual Error load_from_memory(const uint8_t *p_data, size_t p_size, int p_base_size) { return ERR_CANT_CREATE; };
virtual Error bitmap_new(float p_height, float p_ascent, int p_base_size) { return ERR_CANT_CREATE; };
virtual void bitmap_add_texture(const Ref<Texture> &p_texture) { ERR_FAIL_MSG("Not supported."); };
virtual void bitmap_add_char(char32_t p_char, int p_texture_idx, const Rect2 &p_rect, const Size2 &p_align, float p_advance) { ERR_FAIL_MSG("Not supported."); };
virtual void bitmap_add_kerning_pair(char32_t p_A, char32_t p_B, int p_kerning) { ERR_FAIL_MSG("Not supported."); };
virtual float get_height(int p_size) const = 0;
virtual float get_ascent(int p_size) const = 0;
virtual float get_descent(int p_size) const = 0;
virtual float get_underline_position(int p_size) const = 0;
virtual float get_underline_thickness(int p_size) const = 0;
virtual int get_spacing_space() const { return spacing_space; };
virtual void set_spacing_space(int p_value) {
spacing_space = p_value;
clear_cache();
};
virtual int get_spacing_glyph() const { return spacing_glyph; };
virtual void set_spacing_glyph(int p_value) {
spacing_glyph = p_value;
clear_cache();
};
virtual void set_antialiased(bool p_antialiased) = 0;
virtual bool get_antialiased() const = 0;
virtual void set_hinting(TextServer::Hinting p_hinting) = 0;
virtual TextServer::Hinting get_hinting() const = 0;
virtual void set_distance_field_hint(bool p_distance_field) = 0;
virtual bool get_distance_field_hint() const = 0;
virtual void set_force_autohinter(bool p_enabeld) = 0;
virtual bool get_force_autohinter() const = 0;
virtual bool has_outline() const = 0;
virtual float get_base_size() const = 0;
virtual bool has_char(char32_t p_char) const = 0;
virtual String get_supported_chars() const = 0;
virtual Vector2 get_advance(char32_t p_char, int p_size) const = 0;
virtual Vector2 get_kerning(char32_t p_char, char32_t p_next, int p_size) const = 0;
virtual Vector2 draw_glyph(RID p_canvas, int p_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color) const = 0;
virtual Vector2 draw_glyph_outline(RID p_canvas, int p_size, int p_outline_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color) const = 0;
virtual bool get_glyph_contours(int p_size, uint32_t p_index, Vector<Vector3> &r_points, Vector<int32_t> &r_contours, bool &r_orientation) const { return false; };
virtual ~FontDataFallback(){};
};
#endif // FONT_FALLBACK_H

File diff suppressed because it is too large Load Diff

View File

@ -39,22 +39,165 @@
#include "servers/text_server.h"
#include "core/templates/rid_owner.h"
#include "core/templates/thread_work_pool.h"
#include "scene/resources/texture.h"
#include "font_fb.h"
#include "modules/modules_enabled.gen.h"
#ifdef MODULE_FREETYPE_ENABLED
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_TRUETYPE_TABLES_H
#include FT_STROKER_H
#include FT_ADVANCES_H
#include FT_MULTIPLE_MASTERS_H
#include FT_BBOX_H
#endif
class TextServerFallback : public TextServer {
GDCLASS(TextServerFallback, TextServer);
_THREAD_SAFE_CLASS_
float oversampling = 1.f;
mutable RID_PtrOwner<FontDataFallback> font_owner;
mutable RID_PtrOwner<ShapedTextData> shaped_owner;
static String interface_name;
static uint32_t interface_features;
// Font cache data.
#ifdef MODULE_FREETYPE_ENABLED
mutable FT_Library library = nullptr;
#endif
const int rect_range = 2;
struct FontTexture {
Image::Format format;
PackedByteArray imgdata;
int texture_w = 0;
int texture_h = 0;
PackedInt32Array offsets;
Ref<ImageTexture> texture;
};
struct FontTexturePosition {
int index = 0;
int x = 0;
int y = 0;
};
struct FontGlyph {
bool found = false;
int texture_idx = -1;
Rect2 rect;
Rect2 uv_rect;
Vector2 advance;
};
struct FontDataForSizeFallback {
real_t ascent = 0.f;
real_t descent = 0.f;
real_t underline_position = 0.f;
real_t underline_thickness = 0.f;
real_t scale = 1.f;
real_t oversampling = 1.f;
int spacing_glyph = 0;
int spacing_space = 0;
Vector2i size;
Vector<FontTexture> textures;
HashMap<int32_t, FontGlyph> glyph_map;
Map<Vector2i, Vector2> kerning_map;
#ifdef MODULE_FREETYPE_ENABLED
FT_Face face = nullptr;
FT_StreamRec stream;
#endif
~FontDataForSizeFallback() {
#ifdef MODULE_FREETYPE_ENABLED
if (face != nullptr) {
FT_Done_Face(face);
}
#endif
}
};
struct FontDataFallback {
Mutex mutex;
bool antialiased = true;
bool msdf = false;
int msdf_range = 14;
int msdf_source_size = 48;
int fixed_size = 0;
bool force_autohinter = false;
TextServer::Hinting hinting = TextServer::HINTING_LIGHT;
Dictionary variation_coordinates;
real_t oversampling = 0.f;
Map<Vector2i, FontDataForSizeFallback *> cache;
bool face_init = false;
Dictionary supported_varaitions;
// Language/script support override.
Map<String, bool> language_support_overrides;
Map<String, bool> script_support_overrides;
PackedByteArray data;
const uint8_t *data_ptr;
size_t data_size;
mutable ThreadWorkPool work_pool;
~FontDataFallback() {
work_pool.finish();
for (const Map<Vector2i, FontDataForSizeFallback *>::Element *E = cache.front(); E; E = E->next()) {
memdelete(E->get());
}
cache.clear();
}
};
_FORCE_INLINE_ FontTexturePosition find_texture_pos_for_glyph(FontDataForSizeFallback *p_data, int p_color_size, Image::Format p_image_format, int p_width, int p_height) const;
#ifdef MODULE_MSDFGEN_ENABLED
_FORCE_INLINE_ FontGlyph rasterize_msdf(FontDataFallback *p_font_data, FontDataForSizeFallback *p_data, int p_pixel_range, int p_rect_margin, FT_Outline *outline, const Vector2 &advance) const;
#endif
#ifdef MODULE_FREETYPE_ENABLED
_FORCE_INLINE_ FontGlyph rasterize_bitmap(FontDataForSizeFallback *p_data, int p_rect_margin, FT_Bitmap bitmap, int yofs, int xofs, const Vector2 &advance) const;
#endif
_FORCE_INLINE_ bool _ensure_glyph(FontDataFallback *p_font_data, const Vector2i &p_size, int32_t p_glyph) const;
_FORCE_INLINE_ bool _ensure_cache_for_size(FontDataFallback *p_font_data, const Vector2i &p_size) const;
_FORCE_INLINE_ void _font_clear_cache(FontDataFallback *p_font_data);
void _generateMTSDF_threaded(uint32_t y, void *p_td) const;
_FORCE_INLINE_ Vector2i _get_size(const FontDataFallback *p_font_data, int p_size) const {
if (p_font_data->msdf) {
return Vector2i(p_font_data->msdf_source_size, 0);
} else if (p_font_data->fixed_size > 0) {
return Vector2i(p_font_data->fixed_size, 0);
} else {
return Vector2i(p_size, 0);
}
}
_FORCE_INLINE_ Vector2i _get_size_outline(const FontDataFallback *p_font_data, const Vector2i &p_size) const {
if (p_font_data->msdf) {
return Vector2i(p_font_data->msdf_source_size, 0);
} else if (p_font_data->fixed_size > 0) {
return Vector2i(p_font_data->fixed_size, MIN(p_size.y, 1));
} else {
return p_size;
}
}
// Common data.
real_t oversampling = 1.f;
mutable RID_PtrOwner<FontDataFallback> font_owner;
mutable RID_PtrOwner<ShapedTextData> shaped_owner;
protected:
static void _bind_methods(){};
@ -77,72 +220,130 @@ public:
virtual bool is_locale_right_to_left(const String &p_locale) override;
virtual int32_t name_to_tag(const String &p_name) const override;
virtual String tag_to_name(int32_t p_tag) const override;
/* Font interface */
virtual RID create_font_system(const String &p_name, int p_base_size = 16) override;
virtual RID create_font_resource(const String &p_filename, int p_base_size = 16) override;
virtual RID create_font_memory(const uint8_t *p_data, size_t p_size, const String &p_type, int p_base_size = 16) override;
virtual RID create_font_bitmap(float p_height, float p_ascent, int p_base_size = 16) override;
virtual RID create_font() override;
virtual void font_bitmap_add_texture(RID p_font, const Ref<Texture> &p_texture) override;
virtual void font_bitmap_add_char(RID p_font, char32_t p_char, int p_texture_idx, const Rect2 &p_rect, const Size2 &p_align, float p_advance) override;
virtual void font_bitmap_add_kerning_pair(RID p_font, char32_t p_A, char32_t p_B, int p_kerning) override;
virtual void font_set_data(RID p_font_rid, const PackedByteArray &p_data) override;
virtual void font_set_data_ptr(RID p_font_rid, const uint8_t *p_data_ptr, size_t p_data_size) override;
virtual float font_get_height(RID p_font, int p_size) const override;
virtual float font_get_ascent(RID p_font, int p_size) const override;
virtual float font_get_descent(RID p_font, int p_size) const override;
virtual void font_set_antialiased(RID p_font_rid, bool p_antialiased) override;
virtual bool font_is_antialiased(RID p_font_rid) const override;
virtual float font_get_underline_position(RID p_font, int p_size) const override;
virtual float font_get_underline_thickness(RID p_font, int p_size) const override;
virtual void font_set_multichannel_signed_distance_field(RID p_font_rid, bool p_msdf) override;
virtual bool font_is_multichannel_signed_distance_field(RID p_font_rid) const override;
virtual int font_get_spacing_space(RID p_font) const override;
virtual void font_set_spacing_space(RID p_font, int p_value) override;
virtual void font_set_msdf_pixel_range(RID p_font_rid, int p_msdf_pixel_range) override;
virtual int font_get_msdf_pixel_range(RID p_font_rid) const override;
virtual int font_get_spacing_glyph(RID p_font) const override;
virtual void font_set_spacing_glyph(RID p_font, int p_value) override;
virtual void font_set_msdf_size(RID p_font_rid, int p_msdf_size) override;
virtual int font_get_msdf_size(RID p_font_rid) const override;
virtual void font_set_antialiased(RID p_font, bool p_antialiased) override;
virtual bool font_get_antialiased(RID p_font) const override;
virtual void font_set_fixed_size(RID p_font_rid, int p_fixed_size) override;
virtual int font_get_fixed_size(RID p_font_rid) const override;
virtual void font_set_hinting(RID p_font, Hinting p_hinting) override;
virtual Hinting font_get_hinting(RID p_font) const override;
virtual void font_set_force_autohinter(RID p_font_rid, bool p_force_autohinter) override;
virtual bool font_is_force_autohinter(RID p_font_rid) const override;
virtual void font_set_force_autohinter(RID p_font, bool p_enabeld) override;
virtual bool font_get_force_autohinter(RID p_font) const override;
virtual void font_set_hinting(RID p_font_rid, TextServer::Hinting p_hinting) override;
virtual TextServer::Hinting font_get_hinting(RID p_font_rid) const override;
virtual bool font_has_char(RID p_font, char32_t p_char) const override;
virtual String font_get_supported_chars(RID p_font) const override;
virtual void font_set_variation_coordinates(RID p_font_rid, const Dictionary &p_variation_coordinates) override;
virtual Dictionary font_get_variation_coordinates(RID p_font_rid) const override;
virtual void font_set_distance_field_hint(RID p_font, bool p_distance_field) override;
virtual bool font_get_distance_field_hint(RID p_font) const override;
virtual void font_set_oversampling(RID p_font_rid, real_t p_oversampling) override;
virtual real_t font_get_oversampling(RID p_font_rid) const override;
virtual bool font_has_outline(RID p_font) const override;
virtual float font_get_base_size(RID p_font) const override;
virtual Array font_get_size_cache_list(RID p_font_rid) const override;
virtual void font_clear_size_cache(RID p_font_rid) override;
virtual void font_remove_size_cache(RID p_font_rid, const Vector2i &p_size) override;
virtual bool font_is_language_supported(RID p_font, const String &p_language) const override;
virtual void font_set_language_support_override(RID p_font, const String &p_language, bool p_supported) override;
virtual bool font_get_language_support_override(RID p_font, const String &p_language) override;
virtual void font_remove_language_support_override(RID p_font, const String &p_language) override;
Vector<String> font_get_language_support_overrides(RID p_font) override;
virtual void font_set_ascent(RID p_font_rid, int p_size, real_t p_ascent) override;
virtual real_t font_get_ascent(RID p_font_rid, int p_size) const override;
virtual bool font_is_script_supported(RID p_font, const String &p_script) const override;
virtual void font_set_script_support_override(RID p_font, const String &p_script, bool p_supported) override;
virtual bool font_get_script_support_override(RID p_font, const String &p_script) override;
virtual void font_remove_script_support_override(RID p_font, const String &p_script) override;
Vector<String> font_get_script_support_overrides(RID p_font) override;
virtual void font_set_descent(RID p_font_rid, int p_size, real_t p_descent) override;
virtual real_t font_get_descent(RID p_font_rid, int p_size) const override;
virtual uint32_t font_get_glyph_index(RID p_font, char32_t p_char, char32_t p_variation_selector = 0x0000) const override;
virtual Vector2 font_get_glyph_advance(RID p_font, uint32_t p_index, int p_size) const override;
virtual Vector2 font_get_glyph_kerning(RID p_font, uint32_t p_index_a, uint32_t p_index_b, int p_size) const override;
virtual void font_set_underline_position(RID p_font_rid, int p_size, real_t p_underline_position) override;
virtual real_t font_get_underline_position(RID p_font_rid, int p_size) const override;
virtual Vector2 font_draw_glyph(RID p_font, RID p_canvas, int p_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color = Color(1, 1, 1)) const override;
virtual Vector2 font_draw_glyph_outline(RID p_font, RID p_canvas, int p_size, int p_outline_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color = Color(1, 1, 1)) const override;
virtual void font_set_underline_thickness(RID p_font_rid, int p_size, real_t p_underline_thickness) override;
virtual real_t font_get_underline_thickness(RID p_font_rid, int p_size) const override;
virtual bool font_get_glyph_contours(RID p_font, int p_size, uint32_t p_index, Vector<Vector3> &r_points, Vector<int32_t> &r_contours, bool &r_orientation) const override;
virtual void font_set_scale(RID p_font_rid, int p_size, real_t p_scale) override;
virtual real_t font_get_scale(RID p_font_rid, int p_size) const override;
virtual float font_get_oversampling() const override;
virtual void font_set_oversampling(float p_oversampling) override;
virtual void font_set_spacing(RID p_font_rid, int p_size, SpacingType p_spacing, int p_value) override;
virtual int font_get_spacing(RID p_font_rid, int p_size, SpacingType p_spacing) const override;
virtual Vector<String> get_system_fonts() const override;
virtual int font_get_texture_count(RID p_font_rid, const Vector2i &p_size) const override;
virtual void font_clear_textures(RID p_font_rid, const Vector2i &p_size) override;
virtual void font_remove_texture(RID p_font_rid, const Vector2i &p_size, int p_texture_index) override;
virtual void font_set_texture_image(RID p_font_rid, const Vector2i &p_size, int p_texture_index, const Ref<Image> &p_image) override;
virtual Ref<Image> font_get_texture_image(RID p_font_rid, const Vector2i &p_size, int p_texture_index) const override;
virtual void font_set_texture_offsets(RID p_font_rid, const Vector2i &p_size, int p_texture_index, const PackedInt32Array &p_offset) override;
virtual PackedInt32Array font_get_texture_offsets(RID p_font_rid, const Vector2i &p_size, int p_texture_index) const override;
virtual Array font_get_glyph_list(RID p_font_rid, const Vector2i &p_size) const override;
virtual void font_clear_glyphs(RID p_font_rid, const Vector2i &p_size) override;
virtual void font_remove_glyph(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph) override;
virtual Vector2 font_get_glyph_advance(RID p_font_rid, int p_size, int32_t p_glyph) const override;
virtual void font_set_glyph_advance(RID p_font_rid, int p_size, int32_t p_glyph, const Vector2 &p_advance) override;
virtual Vector2 font_get_glyph_offset(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph) const override;
virtual void font_set_glyph_offset(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph, const Vector2 &p_offset) override;
virtual Vector2 font_get_glyph_size(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph) const override;
virtual void font_set_glyph_size(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph, const Vector2 &p_gl_size) override;
virtual Rect2 font_get_glyph_uv_rect(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph) const override;
virtual void font_set_glyph_uv_rect(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph, const Rect2 &p_uv_rect) override;
virtual int font_get_glyph_texture_idx(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph) const override;
virtual void font_set_glyph_texture_idx(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph, int p_texture_idx) override;
virtual bool font_get_glyph_contours(RID p_font, int p_size, int32_t p_index, Vector<Vector3> &r_points, Vector<int32_t> &r_contours, bool &r_orientation) const override;
virtual Array font_get_kerning_list(RID p_font_rid, int p_size) const override;
virtual void font_clear_kerning_map(RID p_font_rid, int p_size) override;
virtual void font_remove_kerning(RID p_font_rid, int p_size, const Vector2i &p_glyph_pair) override;
virtual void font_set_kerning(RID p_font_rid, int p_size, const Vector2i &p_glyph_pair, const Vector2 &p_kerning) override;
virtual Vector2 font_get_kerning(RID p_font_rid, int p_size, const Vector2i &p_glyph_pair) const override;
virtual int32_t font_get_glyph_index(RID p_font_rid, int p_size, char32_t p_char, char32_t p_variation_selector = 0) const override;
virtual bool font_has_char(RID p_font_rid, char32_t p_char) const override;
virtual String font_get_supported_chars(RID p_font_rid) const override;
virtual void font_render_range(RID p_font, const Vector2i &p_size, char32_t p_start, char32_t p_end) override;
virtual void font_render_glyph(RID p_font_rid, const Vector2i &p_size, int32_t p_index) override;
virtual void font_draw_glyph(RID p_font, RID p_canvas, int p_size, const Vector2 &p_pos, int32_t p_index, const Color &p_color = Color(1, 1, 1)) const override;
virtual void font_draw_glyph_outline(RID p_font, RID p_canvas, int p_size, int p_outline_size, const Vector2 &p_pos, int32_t p_index, const Color &p_color = Color(1, 1, 1)) const override;
virtual bool font_is_language_supported(RID p_font_rid, const String &p_language) const override;
virtual void font_set_language_support_override(RID p_font_rid, const String &p_language, bool p_supported) override;
virtual bool font_get_language_support_override(RID p_font_rid, const String &p_language) override;
virtual void font_remove_language_support_override(RID p_font_rid, const String &p_language) override;
virtual Vector<String> font_get_language_support_overrides(RID p_font_rid) override;
virtual bool font_is_script_supported(RID p_font_rid, const String &p_script) const override;
virtual void font_set_script_support_override(RID p_font_rid, const String &p_script, bool p_supported) override;
virtual bool font_get_script_support_override(RID p_font_rid, const String &p_script) override;
virtual void font_remove_script_support_override(RID p_font_rid, const String &p_script) override;
virtual Vector<String> font_get_script_support_overrides(RID p_font_rid) override;
virtual Dictionary font_supported_feature_list(RID p_font_rid) const override;
virtual Dictionary font_supported_variation_list(RID p_font_rid) const override;
virtual real_t font_get_global_oversampling() const override;
virtual void font_set_global_oversampling(real_t p_oversampling) override;
/* Shaped text buffer interface */
@ -171,14 +372,14 @@ public:
virtual RID shaped_text_substr(RID p_shaped, int p_start, int p_length) const override;
virtual RID shaped_text_get_parent(RID p_shaped) const override;
virtual float shaped_text_fit_to_width(RID p_shaped, float p_width, uint8_t /*JustificationFlag*/ p_jst_flags = JUSTIFICATION_WORD_BOUND | JUSTIFICATION_KASHIDA) override;
virtual float shaped_text_tab_align(RID p_shaped, const Vector<float> &p_tab_stops) override;
virtual real_t shaped_text_fit_to_width(RID p_shaped, real_t p_width, uint8_t /*JustificationFlag*/ p_jst_flags = JUSTIFICATION_WORD_BOUND | JUSTIFICATION_KASHIDA) override;
virtual real_t shaped_text_tab_align(RID p_shaped, const Vector<real_t> &p_tab_stops) override;
virtual bool shaped_text_shape(RID p_shaped) override;
virtual bool shaped_text_update_breaks(RID p_shaped) override;
virtual bool shaped_text_update_justification_ops(RID p_shaped) override;
virtual void shaped_text_overrun_trim_to_width(RID p_shaped, float p_width, uint8_t p_trim_flags) override;
virtual void shaped_text_overrun_trim_to_width(RID p_shaped, real_t p_width, uint8_t p_trim_flags) override;
virtual TrimData shaped_text_get_trim_data(RID p_shaped) const override;
virtual bool shaped_text_is_ready(RID p_shaped) const override;
@ -193,17 +394,17 @@ public:
virtual Rect2 shaped_text_get_object_rect(RID p_shaped, Variant p_key) const override;
virtual Size2 shaped_text_get_size(RID p_shaped) const override;
virtual float shaped_text_get_ascent(RID p_shaped) const override;
virtual float shaped_text_get_descent(RID p_shaped) const override;
virtual float shaped_text_get_width(RID p_shaped) const override;
virtual float shaped_text_get_underline_position(RID p_shaped) const override;
virtual float shaped_text_get_underline_thickness(RID p_shaped) const override;
virtual real_t shaped_text_get_ascent(RID p_shaped) const override;
virtual real_t shaped_text_get_descent(RID p_shaped) const override;
virtual real_t shaped_text_get_width(RID p_shaped) const override;
virtual real_t shaped_text_get_underline_position(RID p_shaped) const override;
virtual real_t shaped_text_get_underline_thickness(RID p_shaped) const override;
static TextServer *create_func(Error &r_error, void *p_user_data);
static void register_server();
TextServerFallback(){};
~TextServerFallback(){};
TextServerFallback();
~TextServerFallback();
};
#endif // TEXT_SERVER_FALLBACK_H

View File

@ -66,11 +66,11 @@ bool Label::is_uppercase() const {
int Label::get_line_height(int p_line) const {
Ref<Font> font = get_theme_font(SNAME("font"));
if (p_line >= 0 && p_line < lines_rid.size()) {
return TS->shaped_text_get_size(lines_rid[p_line]).y + font->get_spacing(Font::SPACING_TOP) + font->get_spacing(Font::SPACING_BOTTOM);
return TS->shaped_text_get_size(lines_rid[p_line]).y + font->get_spacing(TextServer::SPACING_TOP) + font->get_spacing(TextServer::SPACING_BOTTOM);
} else if (lines_rid.size() > 0) {
int h = 0;
for (int i = 0; i < lines_rid.size(); i++) {
h = MAX(h, TS->shaped_text_get_size(lines_rid[i]).y) + font->get_spacing(Font::SPACING_TOP) + font->get_spacing(Font::SPACING_BOTTOM);
h = MAX(h, TS->shaped_text_get_size(lines_rid[i]).y) + font->get_spacing(TextServer::SPACING_TOP) + font->get_spacing(TextServer::SPACING_BOTTOM);
}
return h;
} else {
@ -89,7 +89,10 @@ void Label::_shape() {
} else {
TS->shaped_text_set_direction(text_rid, (TextServer::Direction)text_direction);
}
TS->shaped_text_add_string(text_rid, (uppercase) ? xl_text.to_upper() : xl_text, get_theme_font(SNAME("font"))->get_rids(), get_theme_font_size(SNAME("font_size")), opentype_features, (language != "") ? language : TranslationServer::get_singleton()->get_tool_locale());
const Ref<Font> &font = get_theme_font(SNAME("font"));
int font_size = get_theme_font_size(SNAME("font_size"));
ERR_FAIL_COND(font.is_null());
TS->shaped_text_add_string(text_rid, (uppercase) ? xl_text.to_upper() : xl_text, font->get_rids(), font_size, opentype_features, (language != "") ? language : TranslationServer::get_singleton()->get_tool_locale());
TS->shaped_text_set_bidi_override(text_rid, structured_text_parser(st_parser, st_args, xl_text));
dirty = false;
lines_dirty = true;
@ -217,7 +220,7 @@ void Label::_update_visible() {
minsize.height = 0;
int last_line = MIN(lines_rid.size(), lines_visible + lines_skipped);
for (int64_t i = lines_skipped; i < last_line; i++) {
minsize.height += TS->shaped_text_get_size(lines_rid[i]).y + font->get_spacing(Font::SPACING_TOP) + font->get_spacing(Font::SPACING_BOTTOM) + line_spacing;
minsize.height += TS->shaped_text_get_size(lines_rid[i]).y + font->get_spacing(TextServer::SPACING_TOP) + font->get_spacing(TextServer::SPACING_BOTTOM) + line_spacing;
if (minsize.height > (get_size().height - style->get_minimum_size().height + line_spacing)) {
break;
}
@ -286,7 +289,7 @@ void Label::_notification(int p_what) {
// Get number of lines to fit to the height.
for (int64_t i = lines_skipped; i < lines_rid.size(); i++) {
total_h += TS->shaped_text_get_size(lines_rid[i]).y + font->get_spacing(Font::SPACING_TOP) + font->get_spacing(Font::SPACING_BOTTOM) + line_spacing;
total_h += TS->shaped_text_get_size(lines_rid[i]).y + font->get_spacing(TextServer::SPACING_TOP) + font->get_spacing(TextServer::SPACING_BOTTOM) + line_spacing;
if (total_h > (get_size().height - style->get_minimum_size().height + line_spacing)) {
break;
}
@ -302,7 +305,7 @@ void Label::_notification(int p_what) {
// Get real total height.
total_h = 0;
for (int64_t i = lines_skipped; i < last_line; i++) {
total_h += TS->shaped_text_get_size(lines_rid[i]).y + font->get_spacing(Font::SPACING_TOP) + font->get_spacing(Font::SPACING_BOTTOM) + line_spacing;
total_h += TS->shaped_text_get_size(lines_rid[i]).y + font->get_spacing(TextServer::SPACING_TOP) + font->get_spacing(TextServer::SPACING_BOTTOM) + line_spacing;
}
total_h += style->get_margin(SIDE_TOP) + style->get_margin(SIDE_BOTTOM);
@ -357,7 +360,7 @@ void Label::_notification(int p_what) {
for (int i = lines_skipped; i < last_line; i++) {
Size2 line_size = TS->shaped_text_get_size(lines_rid[i]);
ofs.x = 0;
ofs.y += TS->shaped_text_get_ascent(lines_rid[i]) + font->get_spacing(Font::SPACING_TOP);
ofs.y += TS->shaped_text_get_ascent(lines_rid[i]) + font->get_spacing(TextServer::SPACING_TOP);
switch (align) {
case ALIGN_FILL:
if (rtl && autowrap_mode != AUTOWRAP_OFF) {
@ -433,7 +436,7 @@ void Label::_notification(int p_what) {
}
}
}
ofs.y += TS->shaped_text_get_descent(lines_rid[i]) + vsep + line_spacing + font->get_spacing(Font::SPACING_BOTTOM);
ofs.y += TS->shaped_text_get_descent(lines_rid[i]) + vsep + line_spacing + font->get_spacing(TextServer::SPACING_BOTTOM);
}
}
@ -455,7 +458,7 @@ Size2 Label::get_minimum_size() const {
Size2 min_size = minsize;
Ref<Font> font = get_theme_font(SNAME("font"));
min_size.height = MAX(min_size.height, font->get_height(get_theme_font_size(SNAME("font_size"))) + font->get_spacing(Font::SPACING_TOP) + font->get_spacing(Font::SPACING_BOTTOM));
min_size.height = MAX(min_size.height, font->get_height(get_theme_font_size(SNAME("font_size"))) + font->get_spacing(TextServer::SPACING_TOP) + font->get_spacing(TextServer::SPACING_BOTTOM));
Size2 min_style = get_theme_stylebox(SNAME("normal"))->get_minimum_size();
if (autowrap_mode != AUTOWRAP_OFF) {
@ -486,7 +489,7 @@ int Label::get_visible_line_count() const {
int lines_visible = 0;
float total_h = 0.0;
for (int64_t i = lines_skipped; i < lines_rid.size(); i++) {
total_h += TS->shaped_text_get_size(lines_rid[i]).y + font->get_spacing(Font::SPACING_TOP) + font->get_spacing(Font::SPACING_BOTTOM) + line_spacing;
total_h += TS->shaped_text_get_size(lines_rid[i]).y + font->get_spacing(TextServer::SPACING_TOP) + font->get_spacing(TextServer::SPACING_BOTTOM) + line_spacing;
if (total_h > (get_size().height - style->get_minimum_size().height + line_spacing)) {
break;
}

View File

@ -641,7 +641,7 @@ void LineEdit::_notification(int p_what) {
int x_ofs = 0;
bool using_placeholder = text.is_empty() && ime_text.is_empty();
float text_width = TS->shaped_text_get_size(text_rid).x;
float text_height = TS->shaped_text_get_size(text_rid).y + font->get_spacing(Font::SPACING_TOP) + font->get_spacing(Font::SPACING_BOTTOM);
float text_height = TS->shaped_text_get_size(text_rid).y + font->get_spacing(TextServer::SPACING_TOP) + font->get_spacing(TextServer::SPACING_BOTTOM);
switch (align) {
case ALIGN_FILL:
@ -1531,7 +1531,7 @@ Size2 LineEdit::get_minimum_size() const {
min_size.width = MAX(min_size.width, full_width + em_space_size);
}
min_size.height = MAX(TS->shaped_text_get_size(text_rid).y + font->get_spacing(Font::SPACING_TOP) + font->get_spacing(Font::SPACING_BOTTOM), font->get_height(font_size));
min_size.height = MAX(TS->shaped_text_get_size(text_rid).y + font->get_spacing(TextServer::SPACING_TOP) + font->get_spacing(TextServer::SPACING_BOTTOM), font->get_height(font_size));
// Take icons into account.
bool using_placeholder = text.is_empty() && ime_text.is_empty();
@ -1941,6 +1941,7 @@ void LineEdit::_shape() {
const Ref<Font> &font = get_theme_font(SNAME("font"));
int font_size = get_theme_font_size(SNAME("font_size"));
ERR_FAIL_COND(font.is_null());
TS->shaped_text_add_string(text_rid, t, font->get_rids(), font_size, opentype_features, (language != "") ? language : TranslationServer::get_singleton()->get_tool_locale());
TS->shaped_text_set_bidi_override(text_rid, structured_text_parser(st_parser, st_args, t));

View File

@ -49,7 +49,7 @@ public:
Color color;
double elapsed_time = 0.0f;
Dictionary environment;
uint32_t glpyh_index = 0;
uint32_t glyph_index = 0;
RID font;
CharFXTransform();
@ -68,8 +68,8 @@ public:
Color get_color() { return color; }
void set_color(Color p_color) { color = p_color; }
uint32_t get_glyph_index() const { return glpyh_index; };
void set_glyph_index(uint32_t p_glpyh_index) { glpyh_index = p_glpyh_index; };
uint32_t get_glyph_index() const { return glyph_index; };
void set_glyph_index(uint32_t p_glyph_index) { glyph_index = p_glyph_index; };
RID get_font() const { return font; };
void set_font(RID p_font) { font = p_font; };

View File

@ -874,7 +874,7 @@ int RichTextLabel::_draw_line(ItemFrame *p_frame, int p_line, const Vector2 &p_o
charfx->visibility = visible;
charfx->outline = true;
charfx->font = frid;
charfx->glpyh_index = gl;
charfx->glyph_index = gl;
charfx->offset = fx_offset;
charfx->color = font_color;
@ -884,7 +884,7 @@ int RichTextLabel::_draw_line(ItemFrame *p_frame, int p_line, const Vector2 &p_o
fx_offset += charfx->offset;
font_color = charfx->color;
frid = charfx->font;
gl = charfx->glpyh_index;
gl = charfx->glyph_index;
visible &= charfx->visibility;
}
} else if (item_fx->type == ITEM_SHAKE) {
@ -1026,7 +1026,7 @@ int RichTextLabel::_draw_line(ItemFrame *p_frame, int p_line, const Vector2 &p_o
charfx->visibility = visible;
charfx->outline = false;
charfx->font = frid;
charfx->glpyh_index = gl;
charfx->glyph_index = gl;
charfx->offset = fx_offset;
charfx->color = font_color;
@ -1036,7 +1036,7 @@ int RichTextLabel::_draw_line(ItemFrame *p_frame, int p_line, const Vector2 &p_o
fx_offset += charfx->offset;
font_color = charfx->color;
frid = charfx->font;
gl = charfx->glpyh_index;
gl = charfx->glyph_index;
visible &= charfx->visibility;
}
} else if (item_fx->type == ITEM_SHAKE) {

View File

@ -888,7 +888,7 @@ void TextEdit::_notification(int p_what) {
// Draw line.
RID rid = ldata->get_line_rid(line_wrap_index);
float text_height = TS->shaped_text_get_size(rid).y + font->get_spacing(Font::SPACING_TOP) + font->get_spacing(Font::SPACING_BOTTOM);
float text_height = TS->shaped_text_get_size(rid).y + font->get_spacing(TextServer::SPACING_TOP) + font->get_spacing(TextServer::SPACING_BOTTOM);
if (rtl) {
char_margin = size.width - char_margin - TS->shaped_text_get_size(rid).x;

View File

@ -889,11 +889,22 @@ void TreeItem::set_custom_font(int p_column, const Ref<Font> &p_font) {
ERR_FAIL_INDEX(p_column, cells.size());
cells.write[p_column].custom_font = p_font;
}
Ref<Font> TreeItem::get_custom_font(int p_column) const {
ERR_FAIL_INDEX_V(p_column, cells.size(), Ref<Font>());
return cells[p_column].custom_font;
}
void TreeItem::set_custom_font_size(int p_column, int p_font_size) {
ERR_FAIL_INDEX(p_column, cells.size());
cells.write[p_column].custom_font_size = p_font_size;
}
int TreeItem::get_custom_font_size(int p_column) const {
ERR_FAIL_INDEX_V(p_column, cells.size(), -1);
return cells[p_column].custom_font_size;
}
void TreeItem::set_tooltip(int p_column, const String &p_tooltip) {
ERR_FAIL_INDEX(p_column, cells.size());
cells.write[p_column].tooltip = p_tooltip;
@ -1132,6 +1143,9 @@ void TreeItem::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_custom_font", "column", "font"), &TreeItem::set_custom_font);
ClassDB::bind_method(D_METHOD("get_custom_font", "column"), &TreeItem::get_custom_font);
ClassDB::bind_method(D_METHOD("set_custom_font_size", "column", "font_size"), &TreeItem::set_custom_font_size);
ClassDB::bind_method(D_METHOD("get_custom_font_size", "column"), &TreeItem::get_custom_font_size);
ClassDB::bind_method(D_METHOD("set_custom_bg_color", "column", "color", "just_outline"), &TreeItem::set_custom_bg_color, DEFVAL(false));
ClassDB::bind_method(D_METHOD("clear_custom_bg_color", "column"), &TreeItem::clear_custom_bg_color);
ClassDB::bind_method(D_METHOD("get_custom_bg_color", "column"), &TreeItem::get_custom_bg_color);
@ -1507,7 +1521,14 @@ void Tree::update_item_cell(TreeItem *p_item, int p_col) {
} else {
font = cache.font;
}
p_item->cells.write[p_col].text_buf->add_string(valtext, font, cache.font_size, p_item->cells[p_col].opentype_features, (p_item->cells[p_col].language != "") ? p_item->cells[p_col].language : TranslationServer::get_singleton()->get_tool_locale());
int font_size;
if (p_item->cells[p_col].custom_font_size > 0) {
font_size = p_item->cells[p_col].custom_font_size;
} else {
font_size = cache.font_size;
}
p_item->cells.write[p_col].text_buf->add_string(valtext, font, font_size, p_item->cells[p_col].opentype_features, (p_item->cells[p_col].language != "") ? p_item->cells[p_col].language : TranslationServer::get_singleton()->get_tool_locale());
TS->shaped_text_set_bidi_override(p_item->cells[p_col].text_buf->get_rid(), structured_text_parser(p_item->cells[p_col].st_parser, p_item->cells[p_col].st_args, valtext));
p_item->cells.write[p_col].dirty = false;
}

View File

@ -114,6 +114,7 @@ private:
Vector<Button> buttons;
Ref<Font> custom_font;
int custom_font_size = -1;
Cell() {
text_buf.instantiate();
@ -299,6 +300,9 @@ public:
void set_custom_font(int p_column, const Ref<Font> &p_font);
Ref<Font> get_custom_font(int p_column) const;
void set_custom_font_size(int p_column, int p_font_size);
int get_custom_font_size(int p_column) const;
void set_custom_bg_color(int p_column, const Color &p_color, bool p_bg_outline = false);
void clear_custom_bg_color(int p_column);
Color get_custom_bg_color(int p_column) const;

View File

@ -571,6 +571,12 @@ void CanvasItem::draw_texture_rect_region(const Ref<Texture2D> &p_texture, const
p_texture->draw_rect_region(canvas_item, p_rect, p_src_rect, p_modulate, p_transpose, p_clip_uv);
}
void CanvasItem::draw_msdf_texture_rect_region(const Ref<Texture2D> &p_texture, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate, double p_outline, double p_pixel_range) {
ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
ERR_FAIL_COND(p_texture.is_null());
RenderingServer::get_singleton()->canvas_item_add_msdf_texture_rect_region(canvas_item, p_rect, p_texture->get_rid(), p_src_rect, p_modulate, p_outline, p_pixel_range);
}
void CanvasItem::draw_style_box(const Ref<StyleBox> &p_style_box, const Rect2 &p_rect) {
ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
@ -881,6 +887,7 @@ void CanvasItem::_bind_methods() {
ClassDB::bind_method(D_METHOD("draw_texture", "texture", "position", "modulate"), &CanvasItem::draw_texture, DEFVAL(Color(1, 1, 1, 1)));
ClassDB::bind_method(D_METHOD("draw_texture_rect", "texture", "rect", "tile", "modulate", "transpose"), &CanvasItem::draw_texture_rect, DEFVAL(Color(1, 1, 1, 1)), DEFVAL(false));
ClassDB::bind_method(D_METHOD("draw_texture_rect_region", "texture", "rect", "src_rect", "modulate", "transpose", "clip_uv"), &CanvasItem::draw_texture_rect_region, DEFVAL(Color(1, 1, 1, 1)), DEFVAL(false), DEFVAL(true));
ClassDB::bind_method(D_METHOD("draw_msdf_texture_rect_region", "texture", "rect", "src_rect", "modulate", "outline", "pixel_range"), &CanvasItem::draw_msdf_texture_rect_region, DEFVAL(Color(1, 1, 1, 1)), DEFVAL(0.0), DEFVAL(4.0));
ClassDB::bind_method(D_METHOD("draw_style_box", "style_box", "rect"), &CanvasItem::draw_style_box);
ClassDB::bind_method(D_METHOD("draw_primitive", "points", "colors", "uvs", "texture", "width"), &CanvasItem::draw_primitive, DEFVAL(Ref<Texture2D>()), DEFVAL(1.0));
ClassDB::bind_method(D_METHOD("draw_polygon", "points", "colors", "uvs", "texture"), &CanvasItem::draw_polygon, DEFVAL(PackedVector2Array()), DEFVAL(Ref<Texture2D>()));

View File

@ -226,6 +226,7 @@ public:
void draw_texture(const Ref<Texture2D> &p_texture, const Point2 &p_pos, const Color &p_modulate = Color(1, 1, 1, 1));
void draw_texture_rect(const Ref<Texture2D> &p_texture, const Rect2 &p_rect, bool p_tile = false, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false);
void draw_texture_rect_region(const Ref<Texture2D> &p_texture, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, bool p_clip_uv = false);
void draw_msdf_texture_rect_region(const Ref<Texture2D> &p_texture, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate = Color(1, 1, 1), double p_outline = 0.0, double p_pixel_range = 4.0);
void draw_style_box(const Ref<StyleBox> &p_style_box, const Rect2 &p_rect);
void draw_primitive(const Vector<Point2> &p_points, const Vector<Color> &p_colors, const Vector<Point2> &p_uvs, Ref<Texture2D> p_texture = Ref<Texture2D>(), real_t p_width = 1);
void draw_polygon(const Vector<Point2> &p_points, const Vector<Color> &p_colors, const Vector<Point2> &p_uvs = Vector<Point2>(), Ref<Texture2D> p_texture = Ref<Texture2D>());

View File

@ -659,8 +659,8 @@ void Window::_update_viewport_size() {
if (!use_font_oversampling) {
font_oversampling = 1.0;
}
if (TS->font_get_oversampling() != font_oversampling) {
TS->font_set_oversampling(font_oversampling);
if (TS->font_get_global_oversampling() != font_oversampling) {
TS->font_set_global_oversampling(font_oversampling);
}
}

View File

@ -247,12 +247,6 @@
static Ref<ResourceFormatSaverText> resource_saver_text;
static Ref<ResourceFormatLoaderText> resource_loader_text;
static Ref<ResourceFormatLoaderFont> resource_loader_font;
#ifndef DISABLE_DEPRECATED
static Ref<ResourceFormatLoaderCompatFont> resource_loader_compat_font;
#endif /* DISABLE_DEPRECATED */
static Ref<ResourceFormatLoaderStreamTexture2D> resource_loader_stream_texture;
static Ref<ResourceFormatLoaderStreamTextureLayered> resource_loader_texture_layered;
static Ref<ResourceFormatLoaderStreamTexture3D> resource_loader_texture_3d;
@ -267,14 +261,6 @@ void register_scene_types() {
Node::init_node_hrcr();
resource_loader_font.instantiate();
ResourceLoader::add_resource_format_loader(resource_loader_font);
#ifndef DISABLE_DEPRECATED
resource_loader_compat_font.instantiate();
ResourceLoader::add_resource_format_loader(resource_loader_compat_font);
#endif /* DISABLE_DEPRECATED */
resource_loader_stream_texture.instantiate();
ResourceLoader::add_resource_format_loader(resource_loader_stream_texture);
@ -1065,14 +1051,6 @@ void unregister_scene_types() {
SceneDebugger::deinitialize();
clear_default_theme();
ResourceLoader::remove_resource_format_loader(resource_loader_font);
resource_loader_font.unref();
#ifndef DISABLE_DEPRECATED
ResourceLoader::remove_resource_format_loader(resource_loader_compat_font);
resource_loader_compat_font.unref();
#endif /* DISABLE_DEPRECATED */
ResourceLoader::remove_resource_format_loader(resource_loader_texture_layered);
resource_loader_texture_layered.unref();

View File

@ -1020,8 +1020,9 @@ void make_default_theme(bool p_hidpi, Ref<Font> p_font) {
Ref<FontData> dynamic_font_data;
dynamic_font_data.instantiate();
dynamic_font_data->load_memory(_font_OpenSans_SemiBold, _font_OpenSans_SemiBold_size, "ttf", default_font_size);
dynamic_font_data->set_data_ptr(_font_OpenSans_SemiBold, _font_OpenSans_SemiBold_size);
dynamic_font->add_data(dynamic_font_data);
dynamic_font->set_base_size(default_font_size);
default_font = dynamic_font;
}

File diff suppressed because it is too large Load Diff

View File

@ -41,17 +41,27 @@
class FontData : public Resource {
GDCLASS(FontData, Resource);
RES_BASE_EXTENSION("fontdata");
public:
enum SpacingType {
SPACING_GLYPH,
SPACING_SPACE,
};
// Font source data.
const uint8_t *data_ptr = nullptr;
size_t data_size = 0;
PackedByteArray data;
private:
RID rid;
int base_size = 16;
String path;
bool antialiased = true;
bool msdf = false;
int msdf_pixel_range = 16;
int msdf_size = 48;
int fixed_size = 0;
bool force_autohinter = false;
TextServer::Hinting hinting = TextServer::HINTING_LIGHT;
real_t oversampling = 0.f;
// Cache.
mutable Vector<RID> cache;
_FORCE_INLINE_ void _clear_cache();
_FORCE_INLINE_ void _ensure_rid(int p_cache_index) const;
protected:
static void _bind_methods();
@ -63,79 +73,132 @@ protected:
virtual void reset_state() override;
public:
virtual RID get_rid() const override;
// Font source data.
virtual void set_data_ptr(const uint8_t *p_data, size_t p_size);
virtual void set_data(const PackedByteArray &p_data);
virtual PackedByteArray get_data() const;
void load_resource(const String &p_filename, int p_base_size = 16);
void load_memory(const uint8_t *p_data, size_t p_size, const String &p_type, int p_base_size = 16);
void _load_memory(const PackedByteArray &p_data, const String &p_type, int p_base_size = 16);
// Common properties.
virtual void set_antialiased(bool p_antialiased);
virtual bool is_antialiased() const;
void new_bitmap(float p_height, float p_ascent, int p_base_size = 16);
virtual void set_multichannel_signed_distance_field(bool p_msdf);
virtual bool is_multichannel_signed_distance_field() const;
void bitmap_add_texture(const Ref<Texture> &p_texture);
void bitmap_add_char(char32_t p_char, int p_texture_idx, const Rect2 &p_rect, const Size2 &p_align, float p_advance);
void bitmap_add_kerning_pair(char32_t p_A, char32_t p_B, int p_kerning);
virtual void set_msdf_pixel_range(int p_msdf_pixel_range);
virtual int get_msdf_pixel_range() const;
void set_data_path(const String &p_path);
String get_data_path() const;
virtual void set_msdf_size(int p_msdf_size);
virtual int get_msdf_size() const;
float get_height(int p_size) const;
float get_ascent(int p_size) const;
float get_descent(int p_size) const;
virtual void set_fixed_size(int p_fixed_size);
virtual int get_fixed_size() const;
Dictionary get_feature_list() const;
Dictionary get_variation_list() const;
virtual void set_force_autohinter(bool p_force_autohinter);
virtual bool is_force_autohinter() const;
void set_variation(const String &p_name, double p_value);
double get_variation(const String &p_name) const;
virtual void set_hinting(TextServer::Hinting p_hinting);
virtual TextServer::Hinting get_hinting() const;
float get_underline_position(int p_size) const;
float get_underline_thickness(int p_size) const;
virtual void set_oversampling(real_t p_oversampling);
virtual real_t get_oversampling() const;
int get_spacing(int p_type) const;
void set_spacing(int p_type, int p_value);
// Cache.
virtual RID find_cache(const Dictionary &p_variation_coordinates) const;
void set_antialiased(bool p_antialiased);
bool get_antialiased() const;
virtual int get_cache_count() const;
virtual void clear_cache();
virtual void remove_cache(int p_cache_index);
void set_distance_field_hint(bool p_distance_field);
bool get_distance_field_hint() const;
virtual Array get_size_cache_list(int p_cache_index) const;
virtual void clear_size_cache(int p_cache_index);
virtual void remove_size_cache(int p_cache_index, const Vector2i &p_size);
void set_force_autohinter(bool p_enabeld);
bool get_force_autohinter() const;
virtual void set_variation_coordinates(int p_cache_index, const Dictionary &p_variation_coordinates);
virtual Dictionary get_variation_coordinates(int p_cache_index) const;
void set_hinting(TextServer::Hinting p_hinting);
TextServer::Hinting get_hinting() const;
virtual void set_ascent(int p_cache_index, int p_size, real_t p_ascent);
virtual real_t get_ascent(int p_cache_index, int p_size) const;
bool has_char(char32_t p_char) const;
String get_supported_chars() const;
virtual void set_descent(int p_cache_index, int p_size, real_t p_descent);
virtual real_t get_descent(int p_cache_index, int p_size) const;
Vector2 get_glyph_advance(uint32_t p_index, int p_size) const;
Vector2 get_glyph_kerning(uint32_t p_index_a, uint32_t p_index_b, int p_size) const;
virtual void set_underline_position(int p_cache_index, int p_size, real_t p_underline_position);
virtual real_t get_underline_position(int p_cache_index, int p_size) const;
bool has_outline() const;
float get_base_size() const;
virtual void set_underline_thickness(int p_cache_index, int p_size, real_t p_underline_thickness);
virtual real_t get_underline_thickness(int p_cache_index, int p_size) const;
bool is_language_supported(const String &p_language) const;
void set_language_support_override(const String &p_language, bool p_supported);
bool get_language_support_override(const String &p_language) const;
void remove_language_support_override(const String &p_language);
Vector<String> get_language_support_overrides() const;
virtual void set_scale(int p_cache_index, int p_size, real_t p_scale); // Rendering scale for bitmap fonts (e.g. emoji fonts).
virtual real_t get_scale(int p_cache_index, int p_size) const;
bool is_script_supported(const String &p_script) const;
void set_script_support_override(const String &p_script, bool p_supported);
bool get_script_support_override(const String &p_script) const;
void remove_script_support_override(const String &p_script);
Vector<String> get_script_support_overrides() const;
virtual void set_spacing(int p_cache_index, int p_size, TextServer::SpacingType p_spacing, int p_value);
virtual int get_spacing(int p_cache_index, int p_size, TextServer::SpacingType p_spacing) const;
uint32_t get_glyph_index(char32_t p_char, char32_t p_variation_selector = 0x0000) const;
virtual int get_texture_count(int p_cache_index, const Vector2i &p_size) const;
virtual void clear_textures(int p_cache_index, const Vector2i &p_size);
virtual void remove_texture(int p_cache_index, const Vector2i &p_size, int p_texture_index);
Vector2 draw_glyph(RID p_canvas, int p_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color = Color(1, 1, 1)) const;
Vector2 draw_glyph_outline(RID p_canvas, int p_size, int p_outline_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color = Color(1, 1, 1)) const;
virtual void set_texture_image(int p_cache_index, const Vector2i &p_size, int p_texture_index, const Ref<Image> &p_image);
virtual Ref<Image> get_texture_image(int p_cache_index, const Vector2i &p_size, int p_texture_index) const;
virtual void set_texture_offsets(int p_cache_index, const Vector2i &p_size, int p_texture_index, const PackedInt32Array &p_offset);
virtual PackedInt32Array get_texture_offsets(int p_cache_index, const Vector2i &p_size, int p_texture_index) const;
virtual Array get_glyph_list(int p_cache_index, const Vector2i &p_size) const;
virtual void clear_glyphs(int p_cache_index, const Vector2i &p_size);
virtual void remove_glyph(int p_cache_index, const Vector2i &p_size, int32_t p_glyph);
virtual void set_glyph_advance(int p_cache_index, int p_size, int32_t p_glyph, const Vector2 &p_advance);
virtual Vector2 get_glyph_advance(int p_cache_index, int p_size, int32_t p_glyph) const;
virtual void set_glyph_offset(int p_cache_index, const Vector2i &p_size, int32_t p_glyph, const Vector2 &p_offset);
virtual Vector2 get_glyph_offset(int p_cache_index, const Vector2i &p_size, int32_t p_glyph) const;
virtual void set_glyph_size(int p_cache_index, const Vector2i &p_size, int32_t p_glyph, const Vector2 &p_gl_size);
virtual Vector2 get_glyph_size(int p_cache_index, const Vector2i &p_size, int32_t p_glyph) const;
virtual void set_glyph_uv_rect(int p_cache_index, const Vector2i &p_size, int32_t p_glyph, const Rect2 &p_uv_rect);
virtual Rect2 get_glyph_uv_rect(int p_cache_index, const Vector2i &p_size, int32_t p_glyph) const;
virtual void set_glyph_texture_idx(int p_cache_index, const Vector2i &p_size, int32_t p_glyph, int p_texture_idx);
virtual int get_glyph_texture_idx(int p_cache_index, const Vector2i &p_size, int32_t p_glyph) const;
virtual Array get_kerning_list(int p_cache_index, int p_size) const;
virtual void clear_kerning_map(int p_cache_index, int p_size);
virtual void remove_kerning(int p_cache_index, int p_size, const Vector2i &p_glyph_pair);
virtual void set_kerning(int p_cache_index, int p_size, const Vector2i &p_glyph_pair, const Vector2 &p_kerning);
virtual Vector2 get_kerning(int p_cache_index, int p_size, const Vector2i &p_glyph_pair) const;
virtual void render_range(int p_cache_index, const Vector2i &p_size, char32_t p_start, char32_t p_end);
virtual void render_glyph(int p_cache_index, const Vector2i &p_size, int32_t p_index);
virtual RID get_cache_rid(int p_cache_index) const;
// Language/script support override.
virtual bool is_language_supported(const String &p_language) const;
virtual void set_language_support_override(const String &p_language, bool p_supported);
virtual bool get_language_support_override(const String &p_language) const;
virtual void remove_language_support_override(const String &p_language);
virtual Vector<String> get_language_support_overrides() const;
virtual bool is_script_supported(const String &p_script) const;
virtual void set_script_support_override(const String &p_script, bool p_supported);
virtual bool get_script_support_override(const String &p_script) const;
virtual void remove_script_support_override(const String &p_script);
virtual Vector<String> get_script_support_overrides() const;
// Base font properties.
virtual bool has_char(char32_t p_char) const;
virtual String get_supported_chars() const;
virtual int32_t get_glyph_index(int p_size, char32_t p_char, char32_t p_variation_selector = 0x0000) const;
virtual Dictionary get_supported_feature_list() const;
virtual Dictionary get_supported_variation_list() const;
FontData();
FontData(const String &p_filename, int p_base_size);
FontData(const PackedByteArray &p_data, const String &p_type, int p_base_size);
~FontData();
};
@ -147,20 +210,22 @@ class TextParagraph;
class Font : public Resource {
GDCLASS(Font, Resource);
public:
enum SpacingType {
SPACING_TOP,
SPACING_BOTTOM,
};
private:
int spacing_top = 0;
int spacing_bottom = 0;
// Shaped string cache.
mutable LRUCache<uint64_t, Ref<TextLine>> cache;
mutable LRUCache<uint64_t, Ref<TextParagraph>> cache_wrap;
// Font data cache.
Vector<Ref<FontData>> data;
mutable Vector<RID> rids;
// Font config.
int base_size = 16;
Dictionary variation_coordinates;
int spacing_bottom = 0;
int spacing_top = 0;
_FORCE_INLINE_ void _data_changed();
_FORCE_INLINE_ void _ensure_rid(int p_index) const; // Find or create cache record.
protected:
static void _bind_methods();
@ -171,41 +236,49 @@ protected:
virtual void reset_state() override;
void _data_changed();
public:
Dictionary get_feature_list() const;
// Font data control.
void add_data(const Ref<FontData> &p_data);
void set_data(int p_idx, const Ref<FontData> &p_data);
int get_data_count() const;
Ref<FontData> get_data(int p_idx) const;
void remove_data(int p_idx);
// Font data.
virtual void add_data(const Ref<FontData> &p_data);
virtual void set_data(int p_idx, const Ref<FontData> &p_data);
virtual int get_data_count() const;
virtual Ref<FontData> get_data(int p_idx) const;
virtual RID get_data_rid(int p_idx) const;
virtual void clear_data();
virtual void remove_data(int p_idx);
float get_height(int p_size = -1) const;
float get_ascent(int p_size = -1) const;
float get_descent(int p_size = -1) const;
// Font configuration.
virtual void set_base_size(int p_size);
virtual int get_base_size() const;
float get_underline_position(int p_size = -1) const;
float get_underline_thickness(int p_size = -1) const;
virtual void set_variation_coordinates(const Dictionary &p_variation_coordinates);
virtual Dictionary get_variation_coordinates() const;
int get_spacing(int p_type) const;
void set_spacing(int p_type, int p_value);
virtual void set_spacing(TextServer::SpacingType p_spacing, int p_value);
virtual int get_spacing(TextServer::SpacingType p_spacing) const;
// Font metrics.
virtual real_t get_height(int p_size = -1) const;
virtual real_t get_ascent(int p_size = -1) const;
virtual real_t get_descent(int p_size = -1) const;
virtual real_t get_underline_position(int p_size = -1) const;
virtual real_t get_underline_thickness(int p_size = -1) const;
// Drawing string.
Size2 get_string_size(const String &p_text, int p_size = -1) const;
Size2 get_multiline_string_size(const String &p_text, float p_width = -1, int p_size = -1, uint8_t p_flags = TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND) const;
virtual Size2 get_string_size(const String &p_text, int p_size = -1, HAlign p_align = HALIGN_LEFT, real_t p_width = -1, uint8_t p_flags = TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND) const;
virtual Size2 get_multiline_string_size(const String &p_text, real_t p_width = -1, int p_size = -1, uint8_t p_flags = TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND) const;
void draw_string(RID p_canvas_item, const Point2 &p_pos, const String &p_text, HAlign p_align = HALIGN_LEFT, float p_width = -1, int p_size = -1, const Color &p_modulate = Color(1, 1, 1), int p_outline_size = 0, const Color &p_outline_modulate = Color(1, 1, 1, 0), uint8_t p_flags = TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND) const;
void draw_multiline_string(RID p_canvas_item, const Point2 &p_pos, const String &p_text, HAlign p_align = HALIGN_LEFT, float p_width = -1, int p_max_lines = -1, int p_size = -1, const Color &p_modulate = Color(1, 1, 1), int p_outline_size = 0, const Color &p_outline_modulate = Color(1, 1, 1, 0), uint8_t p_flags = TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND) const;
virtual void draw_string(RID p_canvas_item, const Point2 &p_pos, const String &p_text, HAlign p_align = HALIGN_LEFT, real_t p_width = -1, int p_size = -1, const Color &p_modulate = Color(1, 1, 1), int p_outline_size = 0, const Color &p_outline_modulate = Color(1, 1, 1, 0), uint8_t p_flags = TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND) const;
virtual void draw_multiline_string(RID p_canvas_item, const Point2 &p_pos, const String &p_text, HAlign p_align = HALIGN_LEFT, real_t p_width = -1, int p_max_lines = -1, int p_size = -1, const Color &p_modulate = Color(1, 1, 1), int p_outline_size = 0, const Color &p_outline_modulate = Color(1, 1, 1, 0), uint8_t p_flags = TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND) const;
// Helper functions.
bool has_char(char32_t p_char) const;
String get_supported_chars() const;
virtual bool has_char(char32_t p_char) const;
virtual String get_supported_chars() const;
Size2 get_char_size(char32_t p_char, char32_t p_next = 0, int p_size = -1) const;
float draw_char(RID p_canvas_item, const Point2 &p_pos, char32_t p_char, char32_t p_next = 0, int p_size = -1, const Color &p_modulate = Color(1, 1, 1), int p_outline_size = 0, const Color &p_outline_modulate = Color(1, 1, 1, 0)) const;
// Drawing char.
virtual Size2 get_char_size(char32_t p_char, char32_t p_next = 0, int p_size = -1) const;
virtual real_t draw_char(RID p_canvas_item, const Point2 &p_pos, char32_t p_char, char32_t p_next = 0, int p_size = -1, const Color &p_modulate = Color(1, 1, 1), int p_outline_size = 0, const Color &p_outline_modulate = Color(1, 1, 1, 0)) const;
Vector<RID> get_rids() const;
@ -215,31 +288,4 @@ public:
~Font();
};
VARIANT_ENUM_CAST(FontData::SpacingType);
VARIANT_ENUM_CAST(Font::SpacingType);
/*************************************************************************/
class ResourceFormatLoaderFont : public ResourceFormatLoader {
public:
virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE);
virtual void get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const;
virtual void get_recognized_extensions(List<String> *p_extensions) const;
virtual bool handles_type(const String &p_type) const;
virtual String get_resource_type(const String &p_path) const;
};
#ifndef DISABLE_DEPRECATED
class ResourceFormatLoaderCompatFont : public ResourceFormatLoader {
public:
virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE);
virtual void get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const;
virtual void get_recognized_extensions(List<String> *p_extensions) const;
virtual bool handles_type(const String &p_type) const;
virtual String get_resource_type(const String &p_path) const;
};
#endif /* DISABLE_DEPRECATED */
#endif /* FONT_H */

View File

@ -211,8 +211,8 @@ void TextLine::set_bidi_override(const Vector<Vector2i> &p_override) {
bool TextLine::add_string(const String &p_text, const Ref<Font> &p_fonts, int p_size, const Dictionary &p_opentype_features, const String &p_language) {
ERR_FAIL_COND_V(p_fonts.is_null(), false);
bool res = TS->shaped_text_add_string(rid, p_text, p_fonts->get_rids(), p_size, p_opentype_features, p_language);
spacing_top = p_fonts->get_spacing(Font::SPACING_TOP);
spacing_bottom = p_fonts->get_spacing(Font::SPACING_BOTTOM);
spacing_top = p_fonts->get_spacing(TextServer::SPACING_TOP);
spacing_bottom = p_fonts->get_spacing(TextServer::SPACING_BOTTOM);
dirty = true;
return res;
}
@ -409,8 +409,8 @@ int TextLine::hit_test(float p_coords) const {
TextLine::TextLine(const String &p_text, const Ref<Font> &p_fonts, int p_size, const Dictionary &p_opentype_features, const String &p_language, TextServer::Direction p_direction, TextServer::Orientation p_orientation) {
rid = TS->create_shaped_text(p_direction, p_orientation);
spacing_top = p_fonts->get_spacing(Font::SPACING_TOP);
spacing_bottom = p_fonts->get_spacing(Font::SPACING_BOTTOM);
spacing_top = p_fonts->get_spacing(TextServer::SPACING_TOP);
spacing_bottom = p_fonts->get_spacing(TextServer::SPACING_BOTTOM);
TS->shaped_text_add_string(rid, p_text, p_fonts->get_rids(), p_size, p_opentype_features, p_language);
}

View File

@ -333,8 +333,8 @@ void TextParagraph::clear_dropcap() {
bool TextParagraph::add_string(const String &p_text, const Ref<Font> &p_fonts, int p_size, const Dictionary &p_opentype_features, const String &p_language) {
ERR_FAIL_COND_V(p_fonts.is_null(), false);
bool res = TS->shaped_text_add_string(rid, p_text, p_fonts->get_rids(), p_size, p_opentype_features, p_language);
spacing_top = p_fonts->get_spacing(Font::SPACING_TOP);
spacing_bottom = p_fonts->get_spacing(Font::SPACING_BOTTOM);
spacing_top = p_fonts->get_spacing(TextServer::SPACING_TOP);
spacing_bottom = p_fonts->get_spacing(TextServer::SPACING_BOTTOM);
lines_dirty = true;
return res;
}
@ -829,8 +829,8 @@ void TextParagraph::draw_line_outline(RID p_canvas, const Vector2 &p_pos, int p_
TextParagraph::TextParagraph(const String &p_text, const Ref<Font> &p_fonts, int p_size, const Dictionary &p_opentype_features, const String &p_language, float p_width, TextServer::Direction p_direction, TextServer::Orientation p_orientation) {
rid = TS->create_shaped_text(p_direction, p_orientation);
TS->shaped_text_add_string(rid, p_text, p_fonts->get_rids(), p_size, p_opentype_features, p_language);
spacing_top = p_fonts->get_spacing(Font::SPACING_TOP);
spacing_bottom = p_fonts->get_spacing(Font::SPACING_BOTTOM);
spacing_top = p_fonts->get_spacing(TextServer::SPACING_TOP);
spacing_bottom = p_fonts->get_spacing(TextServer::SPACING_BOTTOM);
width = p_width;
}

View File

@ -806,6 +806,40 @@ void RendererCanvasCull::canvas_item_add_texture_rect(RID p_item, const Rect2 &p
rect->texture = p_texture;
}
void RendererCanvasCull::canvas_item_add_msdf_texture_rect_region(RID p_item, const Rect2 &p_rect, RID p_texture, const Rect2 &p_src_rect, const Color &p_modulate, int p_outline_size, float p_px_range) {
Item *canvas_item = canvas_item_owner.getornull(p_item);
ERR_FAIL_COND(!canvas_item);
Item::CommandRect *rect = canvas_item->alloc_command<Item::CommandRect>();
ERR_FAIL_COND(!rect);
rect->modulate = p_modulate;
rect->rect = p_rect;
rect->texture = p_texture;
rect->source = p_src_rect;
rect->flags = RendererCanvasRender::CANVAS_RECT_REGION | RendererCanvasRender::CANVAS_RECT_MSDF;
if (p_rect.size.x < 0) {
rect->flags |= RendererCanvasRender::CANVAS_RECT_FLIP_H;
rect->rect.size.x = -rect->rect.size.x;
}
if (p_src_rect.size.x < 0) {
rect->flags ^= RendererCanvasRender::CANVAS_RECT_FLIP_H;
rect->source.size.x = -rect->source.size.x;
}
if (p_rect.size.y < 0) {
rect->flags |= RendererCanvasRender::CANVAS_RECT_FLIP_V;
rect->rect.size.y = -rect->rect.size.y;
}
if (p_src_rect.size.y < 0) {
rect->flags ^= RendererCanvasRender::CANVAS_RECT_FLIP_V;
rect->source.size.y = -rect->source.size.y;
}
rect->outline = p_outline_size;
rect->px_range = p_px_range;
}
void RendererCanvasCull::canvas_item_add_texture_rect_region(RID p_item, const Rect2 &p_rect, RID p_texture, const Rect2 &p_src_rect, const Color &p_modulate, bool p_transpose, bool p_clip_uv) {
Item *canvas_item = canvas_item_owner.getornull(p_item);
ERR_FAIL_COND(!canvas_item);

View File

@ -222,6 +222,7 @@ public:
void canvas_item_add_circle(RID p_item, const Point2 &p_pos, float p_radius, const Color &p_color);
void canvas_item_add_texture_rect(RID p_item, const Rect2 &p_rect, RID p_texture, bool p_tile = false, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false);
void canvas_item_add_texture_rect_region(RID p_item, const Rect2 &p_rect, RID p_texture, const Rect2 &p_src_rect, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, bool p_clip_uv = false);
void canvas_item_add_msdf_texture_rect_region(RID p_item, const Rect2 &p_rect, RID p_texture, const Rect2 &p_src_rect, const Color &p_modulate = Color(1, 1, 1), int p_outline_size = 0, float p_px_range = 1.0);
void canvas_item_add_nine_patch(RID p_item, const Rect2 &p_rect, const Rect2 &p_source, RID p_texture, const Vector2 &p_topleft, const Vector2 &p_bottomright, RS::NinePatchAxisMode p_x_axis_mode = RS::NINE_PATCH_STRETCH, RS::NinePatchAxisMode p_y_axis_mode = RS::NINE_PATCH_STRETCH, bool p_draw_center = true, const Color &p_modulate = Color(1, 1, 1));
void canvas_item_add_primitive(RID p_item, const Vector<Point2> &p_points, const Vector<Color> &p_colors, const Vector<Point2> &p_uvs, RID p_texture, float p_width = 1.0);
void canvas_item_add_polygon(RID p_item, const Vector<Point2> &p_points, const Vector<Color> &p_colors, const Vector<Point2> &p_uvs = Vector<Point2>(), RID p_texture = RID());

View File

@ -45,6 +45,7 @@ public:
CANVAS_RECT_TRANSPOSE = 16,
CANVAS_RECT_CLIP_UV = 32,
CANVAS_RECT_IS_GROUP = 64,
CANVAS_RECT_MSDF = 128,
};
struct Light {
@ -193,11 +194,15 @@ public:
Color modulate;
Rect2 source;
uint8_t flags;
float outline;
float px_range;
RID texture;
CommandRect() {
flags = 0;
outline = 0;
px_range = 1;
type = TYPE_RECT;
}
};

View File

@ -541,6 +541,14 @@ void RendererCanvasRenderRD::_render_item(RD::DrawListID p_draw_list, RID p_rend
src_rect = Rect2(0, 0, 1, 1);
}
if (rect->flags & CANVAS_RECT_MSDF) {
push_constant.flags |= FLAGS_USE_MSDF;
push_constant.msdf[0] = rect->px_range; // Pixel range.
push_constant.msdf[1] = rect->outline; // Outline size.
push_constant.msdf[2] = 0.f; // Reserved.
push_constant.msdf[3] = 0.f; // Reserved.
}
push_constant.modulation[0] = rect->modulate.r * base_color.r;
push_constant.modulation[1] = rect->modulate.g * base_color.g;
push_constant.modulation[2] = rect->modulate.b * base_color.b;

View File

@ -84,8 +84,9 @@ class RendererCanvasRenderRD : public RendererCanvasRender {
FLAGS_LIGHT_COUNT_SHIFT = 20,
FLAGS_DEFAULT_NORMAL_MAP_USED = (1 << 26),
FLAGS_DEFAULT_SPECULAR_MAP_USED = (1 << 27)
FLAGS_DEFAULT_SPECULAR_MAP_USED = (1 << 27),
FLAGS_USE_MSDF = (1 << 28),
};
enum {
@ -388,7 +389,10 @@ class RendererCanvasRenderRD : public RendererCanvasRender {
//rect
struct {
float modulation[4];
float ninepatch_margins[4];
union {
float msdf[4];
float ninepatch_margins[4];
};
float dst_rect[4];
float src_rect[4];
float pad[2];

View File

@ -458,6 +458,14 @@ void light_blend_compute(uint light_base, vec4 light_color, inout vec3 color) {
#endif
float msdf_median(float r, float g, float b, float a) {
return min(max(min(r, g), min(max(r, g), b)), a);
}
vec2 msdf_map(vec2 value, vec2 in_min, vec2 in_max, vec2 out_min, vec2 out_max) {
return out_min + (out_max - out_min) * (value - in_min) / (in_max - in_min);
}
void main() {
vec4 color = color_interp;
vec2 uv = uv_interp;
@ -485,7 +493,34 @@ void main() {
#endif
color *= texture(sampler2D(color_texture, texture_sampler), uv);
#ifndef USE_PRIMITIVE
if (bool(draw_data.flags & FLAGS_USE_MSDF)) {
float px_range = draw_data.ninepatch_margins.x;
float outline_thickness = draw_data.ninepatch_margins.y;
//float reserved1 = draw_data.ninepatch_margins.z;
//float reserved2 = draw_data.ninepatch_margins.w;
vec4 msdf_sample = texture(sampler2D(color_texture, texture_sampler), uv);
vec2 msdf_size = vec2(textureSize(sampler2D(color_texture, texture_sampler), 0));
vec2 dest_size = vec2(1.0) / fwidth(uv);
float px_size = max(0.5 * dot((vec2(px_range) / msdf_size), dest_size), 1.0);
float d = msdf_median(msdf_sample.r, msdf_sample.g, msdf_sample.b, msdf_sample.a) - 0.5;
if (outline_thickness > 0) {
float cr = clamp(outline_thickness, 0.0, px_range / 2) / px_range;
float a = clamp((d + cr) * px_size, 0.0, 1.0);
color.a = a * color.a;
} else {
float a = clamp(d * px_size + 0.5, 0.0, 1.0);
color.a = a * color.a;
}
} else {
#else
{
#endif
color *= texture(sampler2D(color_texture, texture_sampler), uv);
}
uint light_count = (draw_data.flags >> FLAGS_LIGHT_COUNT_SHIFT) & 0xF; //max 16 lights
bool using_light = light_count > 0 || canvas_data.directional_light_count > 0;

View File

@ -24,6 +24,8 @@
#define FLAGS_DEFAULT_NORMAL_MAP_USED (1 << 26)
#define FLAGS_DEFAULT_SPECULAR_MAP_USED (1 << 27)
#define FLAGS_USE_MSDF (1 << 28)
#define SAMPLER_NEAREST_CLAMP 0
#define SAMPLER_LINEAR_CLAMP 1
#define SAMPLER_NEAREST_WITH_MIPMAPS_CLAMP 2

View File

@ -763,6 +763,7 @@ public:
FUNC4(canvas_item_add_circle, RID, const Point2 &, float, const Color &)
FUNC6(canvas_item_add_texture_rect, RID, const Rect2 &, RID, bool, const Color &, bool)
FUNC7(canvas_item_add_texture_rect_region, RID, const Rect2 &, RID, const Rect2 &, const Color &, bool, bool)
FUNC7(canvas_item_add_msdf_texture_rect_region, RID, const Rect2 &, RID, const Rect2 &, const Color &, int, float)
FUNC10(canvas_item_add_nine_patch, RID, const Rect2 &, const Rect2 &, RID, const Vector2 &, const Vector2 &, NinePatchAxisMode, NinePatchAxisMode, bool, const Color &)
FUNC6(canvas_item_add_primitive, RID, const Vector<Point2> &, const Vector<Color> &, const Vector<Point2> &, RID, float)
FUNC5(canvas_item_add_polygon, RID, const Vector<Point2> &, const Vector<Color> &, const Vector<Point2> &, RID)

View File

@ -1258,6 +1258,7 @@ public:
virtual void canvas_item_add_circle(RID p_item, const Point2 &p_pos, float p_radius, const Color &p_color) = 0;
virtual void canvas_item_add_texture_rect(RID p_item, const Rect2 &p_rect, RID p_texture, bool p_tile = false, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false) = 0;
virtual void canvas_item_add_texture_rect_region(RID p_item, const Rect2 &p_rect, RID p_texture, const Rect2 &p_src_rect, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, bool p_clip_uv = false) = 0;
virtual void canvas_item_add_msdf_texture_rect_region(RID p_item, const Rect2 &p_rect, RID p_texture, const Rect2 &p_src_rect, const Color &p_modulate = Color(1, 1, 1), int p_outline_size = 0, float p_px_range = 1.0) = 0;
virtual void canvas_item_add_nine_patch(RID p_item, const Rect2 &p_rect, const Rect2 &p_source, RID p_texture, const Vector2 &p_topleft, const Vector2 &p_bottomright, NinePatchAxisMode p_x_axis_mode = NINE_PATCH_STRETCH, NinePatchAxisMode p_y_axis_mode = NINE_PATCH_STRETCH, bool p_draw_center = true, const Color &p_modulate = Color(1, 1, 1)) = 0;
virtual void canvas_item_add_primitive(RID p_item, const Vector<Point2> &p_points, const Vector<Color> &p_colors, const Vector<Point2> &p_uvs, RID p_texture, float p_width = 1.0) = 0;
virtual void canvas_item_add_polygon(RID p_item, const Vector<Point2> &p_points, const Vector<Color> &p_colors, const Vector<Point2> &p_uvs = Vector<Point2>(), RID p_texture = RID()) = 0;

View File

@ -216,83 +216,130 @@ void TextServer::_bind_methods() {
ClassDB::bind_method(D_METHOD("free_rid", "rid"), &TextServer::free); // shouldn't conflict with Object::free()
/* Font Interface */
ClassDB::bind_method(D_METHOD("create_font_system", "name", "base_size"), &TextServer::create_font_system, DEFVAL(16));
ClassDB::bind_method(D_METHOD("create_font_resource", "filename", "base_size"), &TextServer::create_font_resource, DEFVAL(16));
ClassDB::bind_method(D_METHOD("create_font_memory", "data", "type", "base_size"), &TextServer::_create_font_memory, DEFVAL(16));
ClassDB::bind_method(D_METHOD("create_font_bitmap", "height", "ascent", "base_size"), &TextServer::create_font_bitmap);
ClassDB::bind_method(D_METHOD("font_bitmap_add_texture", "font", "texture"), &TextServer::font_bitmap_add_texture);
ClassDB::bind_method(D_METHOD("font_bitmap_add_char", "font", "char", "texture_idx", "rect", "align", "advance"), &TextServer::font_bitmap_add_char);
ClassDB::bind_method(D_METHOD("font_bitmap_add_kerning_pair", "font", "A", "B", "kerning"), &TextServer::font_bitmap_add_kerning_pair);
ClassDB::bind_method(D_METHOD("create_font"), &TextServer::create_font);
ClassDB::bind_method(D_METHOD("font_get_height", "font", "size"), &TextServer::font_get_height);
ClassDB::bind_method(D_METHOD("font_get_ascent", "font", "size"), &TextServer::font_get_ascent);
ClassDB::bind_method(D_METHOD("font_get_descent", "font", "size"), &TextServer::font_get_descent);
ClassDB::bind_method(D_METHOD("font_set_data", "data"), &TextServer::font_set_data);
ClassDB::bind_method(D_METHOD("font_get_underline_position", "font", "size"), &TextServer::font_get_underline_position);
ClassDB::bind_method(D_METHOD("font_get_underline_thickness", "font", "size"), &TextServer::font_get_underline_thickness);
ClassDB::bind_method(D_METHOD("font_set_antialiased", "font_rid", "antialiased"), &TextServer::font_set_antialiased);
ClassDB::bind_method(D_METHOD("font_is_antialiased", "font_rid"), &TextServer::font_is_antialiased);
ClassDB::bind_method(D_METHOD("font_get_spacing_space", "font"), &TextServer::font_get_spacing_space);
ClassDB::bind_method(D_METHOD("font_set_spacing_space", "font", "value"), &TextServer::font_set_spacing_space);
ClassDB::bind_method(D_METHOD("font_set_multichannel_signed_distance_field", "font_rid", "msdf"), &TextServer::font_set_multichannel_signed_distance_field);
ClassDB::bind_method(D_METHOD("font_is_multichannel_signed_distance_field", "font_rid"), &TextServer::font_is_multichannel_signed_distance_field);
ClassDB::bind_method(D_METHOD("font_get_spacing_glyph", "font"), &TextServer::font_get_spacing_glyph);
ClassDB::bind_method(D_METHOD("font_set_spacing_glyph", "font", "value"), &TextServer::font_set_spacing_glyph);
ClassDB::bind_method(D_METHOD("font_set_msdf_pixel_range", "font_rid", "msdf_pixel_range"), &TextServer::font_set_msdf_pixel_range);
ClassDB::bind_method(D_METHOD("font_get_msdf_pixel_range", "font_rid"), &TextServer::font_get_msdf_pixel_range);
ClassDB::bind_method(D_METHOD("font_set_antialiased", "font", "antialiased"), &TextServer::font_set_antialiased);
ClassDB::bind_method(D_METHOD("font_get_antialiased", "font"), &TextServer::font_get_antialiased);
ClassDB::bind_method(D_METHOD("font_set_msdf_size", "font_rid", "msdf_size"), &TextServer::font_set_msdf_size);
ClassDB::bind_method(D_METHOD("font_get_msdf_size", "font_rid"), &TextServer::font_get_msdf_size);
ClassDB::bind_method(D_METHOD("font_get_feature_list", "font"), &TextServer::font_get_feature_list);
ClassDB::bind_method(D_METHOD("font_get_variation_list", "font"), &TextServer::font_get_variation_list);
ClassDB::bind_method(D_METHOD("font_set_fixed_size", "font_rid", "fixed_size"), &TextServer::font_set_fixed_size);
ClassDB::bind_method(D_METHOD("font_get_fixed_size", "font_rid"), &TextServer::font_get_fixed_size);
ClassDB::bind_method(D_METHOD("font_set_variation", "font", "tag", "value"), &TextServer::font_set_variation);
ClassDB::bind_method(D_METHOD("font_get_variation", "font", "tag"), &TextServer::font_get_variation);
ClassDB::bind_method(D_METHOD("font_set_force_autohinter", "font_rid", "force_autohinter"), &TextServer::font_set_force_autohinter);
ClassDB::bind_method(D_METHOD("font_is_force_autohinter", "font_rid"), &TextServer::font_is_force_autohinter);
ClassDB::bind_method(D_METHOD("font_set_hinting", "font", "hinting"), &TextServer::font_set_hinting);
ClassDB::bind_method(D_METHOD("font_get_hinting", "font"), &TextServer::font_get_hinting);
ClassDB::bind_method(D_METHOD("font_set_hinting", "font_rid", "_hinting"), &TextServer::font_set_hinting);
ClassDB::bind_method(D_METHOD("font_get_hinting", "font_rid"), &TextServer::font_get_hinting);
ClassDB::bind_method(D_METHOD("font_set_distance_field_hint", "font", "distance_field"), &TextServer::font_set_distance_field_hint);
ClassDB::bind_method(D_METHOD("font_get_distance_field_hint", "font"), &TextServer::font_get_distance_field_hint);
ClassDB::bind_method(D_METHOD("font_set_variation_coordinates", "font_rid", "variation_coordinates"), &TextServer::font_set_variation_coordinates);
ClassDB::bind_method(D_METHOD("font_get_variation_coordinates", "font_rid"), &TextServer::font_get_variation_coordinates);
ClassDB::bind_method(D_METHOD("font_set_force_autohinter", "font", "enabeld"), &TextServer::font_set_force_autohinter);
ClassDB::bind_method(D_METHOD("font_get_force_autohinter", "font"), &TextServer::font_get_force_autohinter);
ClassDB::bind_method(D_METHOD("font_set_oversampling", "font_rid", "oversampling"), &TextServer::font_set_oversampling);
ClassDB::bind_method(D_METHOD("font_get_oversampling", "font_rid"), &TextServer::font_get_oversampling);
ClassDB::bind_method(D_METHOD("font_has_char", "font", "char"), &TextServer::font_has_char);
ClassDB::bind_method(D_METHOD("font_get_supported_chars", "font"), &TextServer::font_get_supported_chars);
ClassDB::bind_method(D_METHOD("font_get_size_cache_list", "font_rid"), &TextServer::font_get_size_cache_list);
ClassDB::bind_method(D_METHOD("font_clear_size_cache", "font_rid"), &TextServer::font_clear_size_cache);
ClassDB::bind_method(D_METHOD("font_remove_size_cache", "font_rid", "size"), &TextServer::font_remove_size_cache);
ClassDB::bind_method(D_METHOD("font_has_outline", "font"), &TextServer::font_has_outline);
ClassDB::bind_method(D_METHOD("font_get_base_size", "font"), &TextServer::font_get_base_size);
ClassDB::bind_method(D_METHOD("font_set_ascent", "font_rid", "size", "ascent"), &TextServer::font_set_ascent);
ClassDB::bind_method(D_METHOD("font_get_ascent", "font_rid", "size"), &TextServer::font_get_ascent);
ClassDB::bind_method(D_METHOD("font_is_language_supported", "font", "language"), &TextServer::font_is_language_supported);
ClassDB::bind_method(D_METHOD("font_set_language_support_override", "font", "language", "supported"), &TextServer::font_set_language_support_override);
ClassDB::bind_method(D_METHOD("font_set_descent", "font_rid", "size", "descent"), &TextServer::font_set_descent);
ClassDB::bind_method(D_METHOD("font_get_descent", "font_rid", "size"), &TextServer::font_get_descent);
ClassDB::bind_method(D_METHOD("font_get_language_support_override", "font", "language"), &TextServer::font_get_language_support_override);
ClassDB::bind_method(D_METHOD("font_remove_language_support_override", "font", "language"), &TextServer::font_remove_language_support_override);
ClassDB::bind_method(D_METHOD("font_get_language_support_overrides", "font"), &TextServer::font_get_language_support_overrides);
ClassDB::bind_method(D_METHOD("font_set_underline_position", "font_rid", "size", "underline_position"), &TextServer::font_set_underline_position);
ClassDB::bind_method(D_METHOD("font_get_underline_position", "font_rid", "size"), &TextServer::font_get_underline_position);
ClassDB::bind_method(D_METHOD("font_is_script_supported", "font", "script"), &TextServer::font_is_script_supported);
ClassDB::bind_method(D_METHOD("font_set_script_support_override", "font", "script", "supported"), &TextServer::font_set_script_support_override);
ClassDB::bind_method(D_METHOD("font_set_underline_thickness", "font_rid", "size", "underline_thickness"), &TextServer::font_set_underline_thickness);
ClassDB::bind_method(D_METHOD("font_get_underline_thickness", "font_rid", "size"), &TextServer::font_get_underline_thickness);
ClassDB::bind_method(D_METHOD("font_get_script_support_override", "font", "script"), &TextServer::font_get_script_support_override);
ClassDB::bind_method(D_METHOD("font_remove_script_support_override", "font", "script"), &TextServer::font_remove_script_support_override);
ClassDB::bind_method(D_METHOD("font_get_script_support_overrides", "font"), &TextServer::font_get_script_support_overrides);
ClassDB::bind_method(D_METHOD("font_set_scale", "font_rid", "size", "scale"), &TextServer::font_set_scale);
ClassDB::bind_method(D_METHOD("font_get_scale", "font_rid", "size"), &TextServer::font_get_scale);
ClassDB::bind_method(D_METHOD("font_get_glyph_index", "font", "char", "variation_selector"), &TextServer::font_get_glyph_index, DEFVAL(0x0000));
ClassDB::bind_method(D_METHOD("font_get_glyph_advance", "font", "index", "size"), &TextServer::font_get_glyph_advance);
ClassDB::bind_method(D_METHOD("font_get_glyph_kerning", "font", "index_a", "index_b", "size"), &TextServer::font_get_glyph_kerning);
ClassDB::bind_method(D_METHOD("font_set_spacing", "font_rid", "size", "spacing", "value"), &TextServer::font_set_spacing);
ClassDB::bind_method(D_METHOD("font_get_spacing", "font_rid", "size", "spacing"), &TextServer::font_get_spacing);
ClassDB::bind_method(D_METHOD("font_draw_glyph", "font", "canvas", "size", "pos", "index", "color"), &TextServer::font_draw_glyph, DEFVAL(Color(1, 1, 1)));
ClassDB::bind_method(D_METHOD("font_draw_glyph_outline", "font", "canvas", "size", "outline_size", "pos", "index", "color"), &TextServer::font_draw_glyph_outline, DEFVAL(Color(1, 1, 1)));
ClassDB::bind_method(D_METHOD("font_get_texture_count", "font_rid", "size"), &TextServer::font_get_texture_count);
ClassDB::bind_method(D_METHOD("font_clear_textures", "font_rid", "size"), &TextServer::font_clear_textures);
ClassDB::bind_method(D_METHOD("font_remove_texture", "font_rid", "size", "texture_index"), &TextServer::font_remove_texture);
ClassDB::bind_method(D_METHOD("font_get_oversampling"), &TextServer::font_get_oversampling);
ClassDB::bind_method(D_METHOD("font_set_oversampling", "oversampling"), &TextServer::font_set_oversampling);
ClassDB::bind_method(D_METHOD("font_set_texture_image", "font_rid", "size", "texture_index", "image"), &TextServer::font_set_texture_image);
ClassDB::bind_method(D_METHOD("font_get_texture_image", "font_rid", "size", "texture_index"), &TextServer::font_get_texture_image);
ClassDB::bind_method(D_METHOD("get_system_fonts"), &TextServer::get_system_fonts);
ClassDB::bind_method(D_METHOD("font_set_texture_offsets", "font_rid", "size", "texture_index", "offset"), &TextServer::font_set_texture_offsets);
ClassDB::bind_method(D_METHOD("font_get_texture_offsets", "font_rid", "size", "texture_index"), &TextServer::font_get_texture_offsets);
ClassDB::bind_method(D_METHOD("font_get_glyph_list", "font_rid", "size"), &TextServer::font_get_glyph_list);
ClassDB::bind_method(D_METHOD("font_clear_glyphs", "font_rid", "size"), &TextServer::font_clear_glyphs);
ClassDB::bind_method(D_METHOD("font_remove_glyph", "font_rid", "size", "glyph"), &TextServer::font_remove_glyph);
ClassDB::bind_method(D_METHOD("font_get_glyph_advance", "font_rid", "size", "glyph"), &TextServer::font_get_glyph_advance);
ClassDB::bind_method(D_METHOD("font_set_glyph_advance", "font_rid", "size", "glyph", "advance"), &TextServer::font_set_glyph_advance);
ClassDB::bind_method(D_METHOD("font_get_glyph_offset", "font_rid", "size", "glyph"), &TextServer::font_get_glyph_offset);
ClassDB::bind_method(D_METHOD("font_set_glyph_offset", "font_rid", "size", "glyph", "offset"), &TextServer::font_set_glyph_offset);
ClassDB::bind_method(D_METHOD("font_get_glyph_size", "font_rid", "size", "glyph"), &TextServer::font_get_glyph_size);
ClassDB::bind_method(D_METHOD("font_set_glyph_size", "font_rid", "size", "glyph", "size"), &TextServer::font_set_glyph_size);
ClassDB::bind_method(D_METHOD("font_get_glyph_uv_rect", "font_rid", "size", "glyph"), &TextServer::font_get_glyph_uv_rect);
ClassDB::bind_method(D_METHOD("font_set_glyph_uv_rect", "font_rid", "size", "glyph", "uv_rect"), &TextServer::font_set_glyph_uv_rect);
ClassDB::bind_method(D_METHOD("font_get_glyph_texture_idx", "font_rid", "size", "glyph"), &TextServer::font_get_glyph_texture_idx);
ClassDB::bind_method(D_METHOD("font_set_glyph_texture_idx", "font_rid", "size", "glyph", "texture_idx"), &TextServer::font_set_glyph_texture_idx);
ClassDB::bind_method(D_METHOD("font_get_glyph_contours", "font", "size", "index"), &TextServer::_font_get_glyph_contours);
ClassDB::bind_method(D_METHOD("font_get_kerning_list", "font_rid", "size"), &TextServer::font_get_kerning_list);
ClassDB::bind_method(D_METHOD("font_clear_kerning_map", "font_rid", "size"), &TextServer::font_clear_kerning_map);
ClassDB::bind_method(D_METHOD("font_remove_kerning", "font_rid", "size", "glyph_pair"), &TextServer::font_remove_kerning);
ClassDB::bind_method(D_METHOD("font_set_kerning", "font_rid", "size", "glyph_pair", "kerning"), &TextServer::font_set_kerning);
ClassDB::bind_method(D_METHOD("font_get_kerning", "font_rid", "size", "glyph_pair"), &TextServer::font_get_kerning);
ClassDB::bind_method(D_METHOD("font_get_glyph_index", "font_rid", "size", "char", "variation_selector"), &TextServer::font_get_glyph_index);
ClassDB::bind_method(D_METHOD("font_has_char", "font_rid", "char"), &TextServer::font_has_char);
ClassDB::bind_method(D_METHOD("font_get_supported_chars", "font_rid"), &TextServer::font_get_supported_chars);
ClassDB::bind_method(D_METHOD("font_render_range", "font_rid", "size", "start", "end"), &TextServer::font_render_range);
ClassDB::bind_method(D_METHOD("font_render_glyph", "font_rid", "size", "index"), &TextServer::font_render_glyph);
ClassDB::bind_method(D_METHOD("font_draw_glyph", "font_rid", "canvas", "size", "pos", "index", "color"), &TextServer::font_draw_glyph, DEFVAL(Color(1, 1, 1)));
ClassDB::bind_method(D_METHOD("font_draw_glyph_outline", "font_rid", "canvas", "size", "outline_size", "pos", "index", "color"), &TextServer::font_draw_glyph_outline, DEFVAL(Color(1, 1, 1)));
ClassDB::bind_method(D_METHOD("font_is_language_supported", "font_rid", "language"), &TextServer::font_is_language_supported);
ClassDB::bind_method(D_METHOD("font_set_language_support_override", "font_rid", "language", "supported"), &TextServer::font_set_language_support_override);
ClassDB::bind_method(D_METHOD("font_get_language_support_override", "font_rid", "language"), &TextServer::font_get_language_support_override);
ClassDB::bind_method(D_METHOD("font_remove_language_support_override", "font_rid", "language"), &TextServer::font_remove_language_support_override);
ClassDB::bind_method(D_METHOD("font_get_language_support_overrides", "font_rid"), &TextServer::font_get_language_support_overrides);
ClassDB::bind_method(D_METHOD("font_is_script_supported", "font_rid", "script"), &TextServer::font_is_script_supported);
ClassDB::bind_method(D_METHOD("font_set_script_support_override", "font_rid", "script", "supported"), &TextServer::font_set_script_support_override);
ClassDB::bind_method(D_METHOD("font_get_script_support_override", "font_rid", "script"), &TextServer::font_get_script_support_override);
ClassDB::bind_method(D_METHOD("font_remove_script_support_override", "font_rid", "script"), &TextServer::font_remove_script_support_override);
ClassDB::bind_method(D_METHOD("font_get_script_support_overrides", "font_rid"), &TextServer::font_get_script_support_overrides);
ClassDB::bind_method(D_METHOD("font_supported_feature_list", "font_rid"), &TextServer::font_supported_feature_list);
ClassDB::bind_method(D_METHOD("font_supported_variation_list", "font_rid"), &TextServer::font_supported_variation_list);
ClassDB::bind_method(D_METHOD("font_get_global_oversampling"), &TextServer::font_get_global_oversampling);
ClassDB::bind_method(D_METHOD("font_set_global_oversampling", "oversampling"), &TextServer::font_set_global_oversampling);
ClassDB::bind_method(D_METHOD("get_hex_code_box_size", "size", "index"), &TextServer::get_hex_code_box_size);
ClassDB::bind_method(D_METHOD("draw_hex_code_box", "canvas", "size", "pos", "index", "color"), &TextServer::draw_hex_code_box);
ClassDB::bind_method(D_METHOD("font_get_glyph_contours", "font", "size", "index"), &TextServer::_font_get_glyph_contours);
/* Shaped text buffer interface */
ClassDB::bind_method(D_METHOD("create_shaped_text", "direction", "orientation"), &TextServer::create_shaped_text, DEFVAL(DIRECTION_AUTO), DEFVAL(ORIENTATION_HORIZONTAL));
@ -420,6 +467,12 @@ void TextServer::_bind_methods() {
BIND_ENUM_CONSTANT(CONTOUR_CURVE_TAG_ON);
BIND_ENUM_CONSTANT(CONTOUR_CURVE_TAG_OFF_CONIC);
BIND_ENUM_CONSTANT(CONTOUR_CURVE_TAG_OFF_CUBIC);
/* Font Spacing*/
BIND_ENUM_CONSTANT(SPACING_GLYPH);
BIND_ENUM_CONSTANT(SPACING_SPACE);
BIND_ENUM_CONSTANT(SPACING_TOP);
BIND_ENUM_CONSTANT(SPACING_BOTTOM);
}
Vector3 TextServer::hex_code_box_font_size[2] = { Vector3(5, 5, 1), Vector3(10, 10, 2) };
@ -514,8 +567,8 @@ void TextServer::finish_hex_code_box_fonts() {
Vector2 TextServer::get_hex_code_box_size(int p_size, char32_t p_index) const {
int fnt = (p_size < 20) ? 0 : 1;
float w = ((p_index <= 0xFF) ? 1 : ((p_index <= 0xFFFF) ? 2 : 3)) * hex_code_box_font_size[fnt].x;
float h = 2 * hex_code_box_font_size[fnt].y;
real_t w = ((p_index <= 0xFF) ? 1 : ((p_index <= 0xFFFF) ? 2 : 3)) * hex_code_box_font_size[fnt].x;
real_t h = 2 * hex_code_box_font_size[fnt].y;
return Vector2(w + 4, h + 3 + 2 * hex_code_box_font_size[fnt].z);
}
@ -534,8 +587,8 @@ void TextServer::draw_hex_code_box(RID p_canvas, int p_size, const Vector2 &p_po
Vector2 pos = p_pos;
Rect2 dest = Rect2(Vector2(), Vector2(hex_code_box_font_size[fnt].x, hex_code_box_font_size[fnt].y));
float w = ((p_index <= 0xFF) ? 1 : ((p_index <= 0xFFFF) ? 2 : 3)) * hex_code_box_font_size[fnt].x;
float h = 2 * hex_code_box_font_size[fnt].y;
real_t w = ((p_index <= 0xFF) ? 1 : ((p_index <= 0xFFFF) ? 2 : 3)) * hex_code_box_font_size[fnt].x;
real_t h = 2 * hex_code_box_font_size[fnt].y;
pos.y -= Math::floor((h + 3 + hex_code_box_font_size[fnt].z) * 0.75);
@ -575,7 +628,7 @@ void TextServer::draw_hex_code_box(RID p_canvas, int p_size, const Vector2 &p_po
}
}
Vector<Vector2i> TextServer::shaped_text_get_line_breaks_adv(RID p_shaped, const Vector<float> &p_width, int p_start, bool p_once, uint8_t /*TextBreakFlag*/ p_break_flags) const {
Vector<Vector2i> TextServer::shaped_text_get_line_breaks_adv(RID p_shaped, const Vector<real_t> &p_width, int p_start, bool p_once, uint8_t /*TextBreakFlag*/ p_break_flags) const {
Vector<Vector2i> lines;
ERR_FAIL_COND_V(p_width.is_empty(), lines);
@ -584,7 +637,7 @@ Vector<Vector2i> TextServer::shaped_text_get_line_breaks_adv(RID p_shaped, const
const Vector<Glyph> &logical = const_cast<TextServer *>(this)->shaped_text_sort_logical(p_shaped);
const Vector2i &range = shaped_text_get_range(p_shaped);
float width = 0.f;
real_t width = 0.f;
int line_start = MAX(p_start, range.x);
int last_safe_break = -1;
int chunk = 0;
@ -646,14 +699,14 @@ Vector<Vector2i> TextServer::shaped_text_get_line_breaks_adv(RID p_shaped, const
return lines;
}
Vector<Vector2i> TextServer::shaped_text_get_line_breaks(RID p_shaped, float p_width, int p_start, uint8_t /*TextBreakFlag*/ p_break_flags) const {
Vector<Vector2i> TextServer::shaped_text_get_line_breaks(RID p_shaped, real_t p_width, int p_start, uint8_t /*TextBreakFlag*/ p_break_flags) const {
Vector<Vector2i> lines;
const_cast<TextServer *>(this)->shaped_text_update_breaks(p_shaped);
const Vector<Glyph> &logical = const_cast<TextServer *>(this)->shaped_text_sort_logical(p_shaped);
const Vector2i &range = shaped_text_get_range(p_shaped);
float width = 0.f;
real_t width = 0.f;
int line_start = MAX(p_start, range.x);
int last_safe_break = -1;
int word_count = 0;
@ -747,11 +800,11 @@ void TextServer::shaped_text_get_carets(RID p_shaped, int p_position, Rect2 &p_l
const Vector<TextServer::Glyph> visual = shaped_text_get_glyphs(p_shaped);
TextServer::Orientation orientation = shaped_text_get_orientation(p_shaped);
const Vector2 &range = shaped_text_get_range(p_shaped);
float ascent = shaped_text_get_ascent(p_shaped);
float descent = shaped_text_get_descent(p_shaped);
float height = (ascent + descent) / 2;
real_t ascent = shaped_text_get_ascent(p_shaped);
real_t descent = shaped_text_get_descent(p_shaped);
real_t height = (ascent + descent) / 2;
float off = 0.0f;
real_t off = 0.0f;
p_leading_dir = DIRECTION_AUTO;
p_trailing_dir = DIRECTION_AUTO;
@ -857,11 +910,11 @@ void TextServer::shaped_text_get_carets(RID p_shaped, int p_position, Rect2 &p_l
}
// Caret inside grapheme (middle).
if (p_position > glyphs[i].start && p_position < glyphs[i].end && (glyphs[i].flags & GRAPHEME_IS_VIRTUAL) != GRAPHEME_IS_VIRTUAL) {
float advance = 0.f;
real_t advance = 0.f;
for (int j = 0; j < glyphs[i].count; j++) {
advance += glyphs[i + j].advance * glyphs[i + j].repeat;
}
float char_adv = advance / (float)(glyphs[i].end - glyphs[i].start);
real_t char_adv = advance / (real_t)(glyphs[i].end - glyphs[i].start);
Rect2 cr;
if (orientation == ORIENTATION_HORIZONTAL) {
cr.size.x = 1.0f;
@ -940,14 +993,14 @@ Vector<Vector2> TextServer::shaped_text_get_selection(RID p_shaped, int p_start,
int v_size = visual.size();
const Glyph *glyphs = visual.ptr();
float off = 0.0f;
real_t off = 0.0f;
for (int i = 0; i < v_size; i++) {
for (int k = 0; k < glyphs[i].repeat; k++) {
if ((glyphs[i].count > 0) && ((glyphs[i].index != 0) || ((glyphs[i].flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE))) {
if (glyphs[i].start < end && glyphs[i].end > start) {
// Grapheme fully in selection range.
if (glyphs[i].start >= start && glyphs[i].end <= end) {
float advance = 0.f;
real_t advance = 0.f;
for (int j = 0; j < glyphs[i].count; j++) {
advance += glyphs[i + j].advance;
}
@ -955,11 +1008,11 @@ Vector<Vector2> TextServer::shaped_text_get_selection(RID p_shaped, int p_start,
}
// Only start of grapheme is in selection range.
if (glyphs[i].start >= start && glyphs[i].end > end) {
float advance = 0.f;
real_t advance = 0.f;
for (int j = 0; j < glyphs[i].count; j++) {
advance += glyphs[i + j].advance;
}
float char_adv = advance / (float)(glyphs[i].end - glyphs[i].start);
real_t char_adv = advance / (real_t)(glyphs[i].end - glyphs[i].start);
if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
ranges.push_back(Vector2(off + char_adv * (glyphs[i].end - end), off + advance));
} else {
@ -968,11 +1021,11 @@ Vector<Vector2> TextServer::shaped_text_get_selection(RID p_shaped, int p_start,
}
// Only end of grapheme is in selection range.
if (glyphs[i].start < start && glyphs[i].end <= end) {
float advance = 0.f;
real_t advance = 0.f;
for (int j = 0; j < glyphs[i].count; j++) {
advance += glyphs[i + j].advance;
}
float char_adv = advance / (float)(glyphs[i].end - glyphs[i].start);
real_t char_adv = advance / (real_t)(glyphs[i].end - glyphs[i].start);
if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
ranges.push_back(Vector2(off, off + char_adv * (start - glyphs[i].start)));
} else {
@ -981,11 +1034,11 @@ Vector<Vector2> TextServer::shaped_text_get_selection(RID p_shaped, int p_start,
}
// Selection range is within grapheme.
if (glyphs[i].start < start && glyphs[i].end > end) {
float advance = 0.f;
real_t advance = 0.f;
for (int j = 0; j < glyphs[i].count; j++) {
advance += glyphs[i + j].advance;
}
float char_adv = advance / (float)(glyphs[i].end - glyphs[i].start);
real_t char_adv = advance / (real_t)(glyphs[i].end - glyphs[i].start);
if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
ranges.push_back(Vector2(off + char_adv * (glyphs[i].end - end), off + char_adv * (glyphs[i].end - start)));
} else {
@ -1020,11 +1073,11 @@ Vector<Vector2> TextServer::shaped_text_get_selection(RID p_shaped, int p_start,
return ranges;
}
int TextServer::shaped_text_hit_test_grapheme(RID p_shaped, float p_coords) const {
int TextServer::shaped_text_hit_test_grapheme(RID p_shaped, real_t p_coords) const {
const Vector<TextServer::Glyph> visual = shaped_text_get_glyphs(p_shaped);
// Exact grapheme hit test, return -1 if missed.
float off = 0.0f;
real_t off = 0.0f;
int v_size = visual.size();
const Glyph *glyphs = visual.ptr();
@ -1040,7 +1093,7 @@ int TextServer::shaped_text_hit_test_grapheme(RID p_shaped, float p_coords) cons
return -1;
}
int TextServer::shaped_text_hit_test_position(RID p_shaped, float p_coords) const {
int TextServer::shaped_text_hit_test_position(RID p_shaped, real_t p_coords) const {
const Vector<TextServer::Glyph> visual = shaped_text_get_glyphs(p_shaped);
int v_size = visual.size();
@ -1074,10 +1127,10 @@ int TextServer::shaped_text_hit_test_position(RID p_shaped, float p_coords) cons
}
}
float off = 0.0f;
real_t off = 0.0f;
for (int i = 0; i < v_size; i++) {
if (glyphs[i].count > 0) {
float advance = 0.f;
real_t advance = 0.f;
for (int j = 0; j < glyphs[i].count; j++) {
advance += glyphs[i + j].advance * glyphs[i + j].repeat;
}
@ -1135,7 +1188,7 @@ int TextServer::shaped_text_prev_grapheme_pos(RID p_shaped, int p_pos) {
return p_pos;
}
void TextServer::shaped_text_draw(RID p_shaped, RID p_canvas, const Vector2 &p_pos, float p_clip_l, float p_clip_r, const Color &p_color) const {
void TextServer::shaped_text_draw(RID p_shaped, RID p_canvas, const Vector2 &p_pos, real_t p_clip_l, real_t p_clip_r, const Color &p_color) const {
const Vector<TextServer::Glyph> visual = shaped_text_get_glyphs(p_shaped);
TextServer::Orientation orientation = shaped_text_get_orientation(p_shaped);
bool hex_codes = shaped_text_get_preserve_control(p_shaped) || shaped_text_get_preserve_invalid(p_shaped);
@ -1228,7 +1281,7 @@ void TextServer::shaped_text_draw(RID p_shaped, RID p_canvas, const Vector2 &p_p
}
}
void TextServer::shaped_text_draw_outline(RID p_shaped, RID p_canvas, const Vector2 &p_pos, float p_clip_l, float p_clip_r, int p_outline_size, const Color &p_color) const {
void TextServer::shaped_text_draw_outline(RID p_shaped, RID p_canvas, const Vector2 &p_pos, real_t p_clip_l, real_t p_clip_r, int p_outline_size, const Color &p_color) const {
const Vector<TextServer::Glyph> visual = shaped_text_get_glyphs(p_shaped);
TextServer::Orientation orientation = shaped_text_get_orientation(p_shaped);
@ -1316,11 +1369,7 @@ void TextServer::shaped_text_draw_outline(RID p_shaped, RID p_canvas, const Vect
}
}
RID TextServer::_create_font_memory(const PackedByteArray &p_data, const String &p_type, int p_base_size) {
return create_font_memory(p_data.ptr(), p_data.size(), p_type, p_base_size);
}
Dictionary TextServer::_font_get_glyph_contours(RID p_font, int p_size, uint32_t p_index) const {
Dictionary TextServer::_font_get_glyph_contours(RID p_font, int p_size, int32_t p_index) const {
Vector<Vector3> points;
Vector<int32_t> contours;
bool orientation;
@ -1378,7 +1427,7 @@ Array TextServer::_shaped_text_get_line_breaks_adv(RID p_shaped, const PackedFlo
return ret;
}
Array TextServer::_shaped_text_get_line_breaks(RID p_shaped, float p_width, int p_start, uint8_t p_break_flags) const {
Array TextServer::_shaped_text_get_line_breaks(RID p_shaped, real_t p_width, int p_start, uint8_t p_break_flags) const {
Array ret;
Vector<Vector2i> lines = shaped_text_get_line_breaks(p_shaped, p_width, p_start, p_break_flags);

View File

@ -116,6 +116,13 @@ public:
CONTOUR_CURVE_TAG_OFF_CUBIC = 0x02
};
enum SpacingType {
SPACING_GLYPH,
SPACING_SPACE,
SPACING_TOP,
SPACING_BOTTOM,
};
struct Glyph {
int start = -1; // Start offset in the source string.
int end = -1; // End offset in the source string.
@ -124,13 +131,13 @@ public:
uint8_t repeat = 1; // Draw multiple times in the row.
uint16_t flags = 0; // Grapheme flags (valid, rtl, virtual), set in the first glyph only.
float x_off = 0.f; // Offset from the origin of the glyph on baseline.
float y_off = 0.f;
float advance = 0.f; // Advance to the next glyph along baseline(x for horizontal layout, y for vertical).
real_t x_off = 0.f; // Offset from the origin of the glyph on baseline.
real_t y_off = 0.f;
real_t advance = 0.f; // Advance to the next glyph along baseline(x for horizontal layout, y for vertical).
RID font_rid; // Font resource.
int font_size = 0; // Font size;
uint32_t index = 0; // Glyph index (font specific) or UTF-32 codepoint (for the invalid glyphs).
int32_t index = 0; // Glyph index (font specific) or UTF-32 codepoint (for the invalid glyphs).
bool operator==(const Glyph &p_a) const;
bool operator!=(const Glyph &p_a) const;
@ -163,6 +170,8 @@ public:
};
struct ShapedTextData {
Mutex mutex;
/* Source data */
RID parent; // Substring parent ShapedTextData.
@ -205,13 +214,13 @@ public:
bool preserve_invalid = true; // Draw hex code box instead of missing characters.
bool preserve_control = false; // Draw control characters.
float ascent = 0.f; // Ascent for horizontal layout, 1/2 of width for vertical.
float descent = 0.f; // Descent for horizontal layout, 1/2 of width for vertical.
float width = 0.f; // Width for horizontal layout, height for vertical.
float width_trimmed = 0.f;
real_t ascent = 0.f; // Ascent for horizontal layout, 1/2 of width for vertical.
real_t descent = 0.f; // Descent for horizontal layout, 1/2 of width for vertical.
real_t width = 0.f; // Width for horizontal layout, height for vertical.
real_t width_trimmed = 0.f;
float upos = 0.f;
float uthk = 0.f;
real_t upos = 0.f;
real_t uthk = 0.f;
TrimData overrun_trim_data;
bool fit_width_minimum_reached = false;
@ -245,85 +254,134 @@ public:
virtual bool is_locale_right_to_left(const String &p_locale) = 0;
virtual int32_t name_to_tag(const String &p_name) { return 0; };
virtual String tag_to_name(int32_t p_tag) { return ""; };
virtual int32_t name_to_tag(const String &p_name) const { return 0; };
virtual String tag_to_name(int32_t p_tag) const { return ""; };
/* Font interface */
virtual RID create_font_system(const String &p_name, int p_base_size = 16) = 0;
virtual RID create_font_resource(const String &p_filename, int p_base_size = 16) = 0;
virtual RID create_font_memory(const uint8_t *p_data, size_t p_size, const String &p_type, int p_base_size = 16) = 0;
virtual RID create_font_bitmap(float p_height, float p_ascent, int p_base_size = 16) = 0;
virtual RID create_font() = 0;
virtual void font_bitmap_add_texture(RID p_font, const Ref<Texture> &p_texture) = 0;
virtual void font_bitmap_add_char(RID p_font, char32_t p_char, int p_texture_idx, const Rect2 &p_rect, const Size2 &p_align, float p_advance) = 0;
virtual void font_bitmap_add_kerning_pair(RID p_font, char32_t p_A, char32_t p_B, int p_kerning) = 0;
virtual void font_set_data(RID p_font_rid, const PackedByteArray &p_data) = 0;
virtual void font_set_data_ptr(RID p_font_rid, const uint8_t *p_data_ptr, size_t p_data_size) = 0;
virtual float font_get_height(RID p_font, int p_size) const = 0;
virtual float font_get_ascent(RID p_font, int p_size) const = 0;
virtual float font_get_descent(RID p_font, int p_size) const = 0;
virtual void font_set_antialiased(RID p_font_rid, bool p_antialiased) = 0;
virtual bool font_is_antialiased(RID p_font_rid) const = 0;
virtual int font_get_spacing_space(RID p_font) const = 0;
virtual void font_set_spacing_space(RID p_font, int p_value) = 0;
virtual void font_set_multichannel_signed_distance_field(RID p_font_rid, bool p_msdf) = 0;
virtual bool font_is_multichannel_signed_distance_field(RID p_font_rid) const = 0;
virtual int font_get_spacing_glyph(RID p_font) const = 0;
virtual void font_set_spacing_glyph(RID p_font, int p_value) = 0;
virtual void font_set_msdf_pixel_range(RID p_font_rid, int p_msdf_pixel_range) = 0;
virtual int font_get_msdf_pixel_range(RID p_font_rid) const = 0;
virtual float font_get_underline_position(RID p_font, int p_size) const = 0;
virtual float font_get_underline_thickness(RID p_font, int p_size) const = 0;
virtual void font_set_msdf_size(RID p_font_rid, int p_msdf_size) = 0;
virtual int font_get_msdf_size(RID p_font_rid) const = 0;
virtual void font_set_antialiased(RID p_font, bool p_antialiased) = 0;
virtual bool font_get_antialiased(RID p_font) const = 0;
virtual void font_set_fixed_size(RID p_font_rid, int p_fixed_size) = 0;
virtual int font_get_fixed_size(RID p_font_rid) const = 0;
virtual Dictionary font_get_feature_list(RID p_font) const { return Dictionary(); };
virtual Dictionary font_get_variation_list(RID p_font) const { return Dictionary(); };
virtual void font_set_force_autohinter(RID p_font_rid, bool p_force_autohinter) = 0;
virtual bool font_is_force_autohinter(RID p_font_rid) const = 0;
virtual void font_set_variation(RID p_font, const String &p_name, double p_value){};
virtual double font_get_variation(RID p_font, const String &p_name) const { return 0; };
virtual void font_set_hinting(RID p_font_rid, TextServer::Hinting p_hinting) = 0;
virtual TextServer::Hinting font_get_hinting(RID p_font_rid) const = 0;
virtual void font_set_distance_field_hint(RID p_font, bool p_distance_field) = 0;
virtual bool font_get_distance_field_hint(RID p_font) const = 0;
virtual void font_set_variation_coordinates(RID p_font_rid, const Dictionary &p_variation_coordinates) = 0;
virtual Dictionary font_get_variation_coordinates(RID p_font_rid) const = 0;
virtual void font_set_hinting(RID p_font, Hinting p_hinting) = 0;
virtual Hinting font_get_hinting(RID p_font) const = 0;
virtual void font_set_oversampling(RID p_font_rid, real_t p_oversampling) = 0;
virtual real_t font_get_oversampling(RID p_font_rid) const = 0;
virtual void font_set_force_autohinter(RID p_font, bool p_enabeld) = 0;
virtual bool font_get_force_autohinter(RID p_font) const = 0;
virtual Array font_get_size_cache_list(RID p_font_rid) const = 0;
virtual void font_clear_size_cache(RID p_font_rid) = 0;
virtual void font_remove_size_cache(RID p_font_rid, const Vector2i &p_size) = 0;
virtual bool font_has_char(RID p_font, char32_t p_char) const = 0;
virtual String font_get_supported_chars(RID p_font) const = 0;
virtual void font_set_ascent(RID p_font_rid, int p_size, real_t p_ascent) = 0;
virtual real_t font_get_ascent(RID p_font_rid, int p_size) const = 0;
virtual bool font_has_outline(RID p_font) const = 0;
virtual float font_get_base_size(RID p_font) const = 0;
virtual void font_set_descent(RID p_font_rid, int p_size, real_t p_descent) = 0;
virtual real_t font_get_descent(RID p_font_rid, int p_size) const = 0;
virtual bool font_is_language_supported(RID p_font, const String &p_language) const = 0;
virtual void font_set_language_support_override(RID p_font, const String &p_language, bool p_supported) = 0;
virtual bool font_get_language_support_override(RID p_font, const String &p_language) = 0;
virtual void font_remove_language_support_override(RID p_font, const String &p_language) = 0;
virtual Vector<String> font_get_language_support_overrides(RID p_font) = 0;
virtual void font_set_underline_position(RID p_font_rid, int p_size, real_t p_underline_position) = 0;
virtual real_t font_get_underline_position(RID p_font_rid, int p_size) const = 0;
virtual bool font_is_script_supported(RID p_font, const String &p_script) const = 0;
virtual void font_set_script_support_override(RID p_font, const String &p_script, bool p_supported) = 0;
virtual bool font_get_script_support_override(RID p_font, const String &p_script) = 0;
virtual void font_remove_script_support_override(RID p_font, const String &p_script) = 0;
virtual Vector<String> font_get_script_support_overrides(RID p_font) = 0;
virtual void font_set_underline_thickness(RID p_font_rid, int p_size, real_t p_underline_thickness) = 0;
virtual real_t font_get_underline_thickness(RID p_font_rid, int p_size) const = 0;
virtual uint32_t font_get_glyph_index(RID p_font, char32_t p_char, char32_t p_variation_selector = 0x0000) const = 0;
virtual Vector2 font_get_glyph_advance(RID p_font, uint32_t p_index, int p_size) const = 0;
virtual Vector2 font_get_glyph_kerning(RID p_font, uint32_t p_index_a, uint32_t p_index_b, int p_size) const = 0;
virtual void font_set_scale(RID p_font_rid, int p_size, real_t p_scale) = 0;
virtual real_t font_get_scale(RID p_font_rid, int p_size) const = 0;
virtual Vector2 font_draw_glyph(RID p_font, RID p_canvas, int p_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color = Color(1, 1, 1)) const = 0;
virtual Vector2 font_draw_glyph_outline(RID p_font, RID p_canvas, int p_size, int p_outline_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color = Color(1, 1, 1)) const = 0;
virtual void font_set_spacing(RID p_font_rid, int p_size, SpacingType p_spacing, int p_value) = 0;
virtual int font_get_spacing(RID p_font_rid, int p_size, SpacingType p_spacing) const = 0;
virtual bool font_get_glyph_contours(RID p_font, int p_size, uint32_t p_index, Vector<Vector3> &r_points, Vector<int32_t> &r_contours, bool &r_orientation) const = 0;
virtual int font_get_texture_count(RID p_font_rid, const Vector2i &p_size) const = 0;
virtual void font_clear_textures(RID p_font_rid, const Vector2i &p_size) = 0;
virtual void font_remove_texture(RID p_font_rid, const Vector2i &p_size, int p_texture_index) = 0;
virtual float font_get_oversampling() const = 0;
virtual void font_set_oversampling(float p_oversampling) = 0;
virtual void font_set_texture_image(RID p_font_rid, const Vector2i &p_size, int p_texture_index, const Ref<Image> &p_image) = 0;
virtual Ref<Image> font_get_texture_image(RID p_font_rid, const Vector2i &p_size, int p_texture_index) const = 0;
virtual void font_set_texture_offsets(RID p_font_rid, const Vector2i &p_size, int p_texture_index, const PackedInt32Array &p_offset) = 0;
virtual PackedInt32Array font_get_texture_offsets(RID p_font_rid, const Vector2i &p_size, int p_texture_index) const = 0;
virtual Array font_get_glyph_list(RID p_font_rid, const Vector2i &p_size) const = 0;
virtual void font_clear_glyphs(RID p_font_rid, const Vector2i &p_size) = 0;
virtual void font_remove_glyph(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph) = 0;
virtual Vector2 font_get_glyph_advance(RID p_font_rid, int p_size, int32_t p_glyph) const = 0;
virtual void font_set_glyph_advance(RID p_font_rid, int p_size, int32_t p_glyph, const Vector2 &p_advance) = 0;
virtual Vector2 font_get_glyph_offset(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph) const = 0;
virtual void font_set_glyph_offset(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph, const Vector2 &p_offset) = 0;
virtual Vector2 font_get_glyph_size(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph) const = 0;
virtual void font_set_glyph_size(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph, const Vector2 &p_gl_size) = 0;
virtual Rect2 font_get_glyph_uv_rect(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph) const = 0;
virtual void font_set_glyph_uv_rect(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph, const Rect2 &p_uv_rect) = 0;
virtual int font_get_glyph_texture_idx(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph) const = 0;
virtual void font_set_glyph_texture_idx(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph, int p_texture_idx) = 0;
virtual bool font_get_glyph_contours(RID p_font, int p_size, int32_t p_index, Vector<Vector3> &r_points, Vector<int32_t> &r_contours, bool &r_orientation) const = 0;
virtual Array font_get_kerning_list(RID p_font_rid, int p_size) const = 0;
virtual void font_clear_kerning_map(RID p_font_rid, int p_size) = 0;
virtual void font_remove_kerning(RID p_font_rid, int p_size, const Vector2i &p_glyph_pair) = 0;
virtual void font_set_kerning(RID p_font_rid, int p_size, const Vector2i &p_glyph_pair, const Vector2 &p_kerning) = 0;
virtual Vector2 font_get_kerning(RID p_font_rid, int p_size, const Vector2i &p_glyph_pair) const = 0;
virtual int32_t font_get_glyph_index(RID p_font_rid, int p_size, char32_t p_char, char32_t p_variation_selector) const = 0;
virtual bool font_has_char(RID p_font_rid, char32_t p_char) const = 0;
virtual String font_get_supported_chars(RID p_font_rid) const = 0;
virtual void font_render_range(RID p_font, const Vector2i &p_size, char32_t p_start, char32_t p_end) = 0;
virtual void font_render_glyph(RID p_font_rid, const Vector2i &p_size, int32_t p_index) = 0;
virtual void font_draw_glyph(RID p_font, RID p_canvas, int p_size, const Vector2 &p_pos, int32_t p_index, const Color &p_color = Color(1, 1, 1)) const = 0;
virtual void font_draw_glyph_outline(RID p_font, RID p_canvas, int p_size, int p_outline_size, const Vector2 &p_pos, int32_t p_index, const Color &p_color = Color(1, 1, 1)) const = 0;
virtual bool font_is_language_supported(RID p_font_rid, const String &p_language) const = 0;
virtual void font_set_language_support_override(RID p_font_rid, const String &p_language, bool p_supported) = 0;
virtual bool font_get_language_support_override(RID p_font_rid, const String &p_language) = 0;
virtual void font_remove_language_support_override(RID p_font_rid, const String &p_language) = 0;
virtual Vector<String> font_get_language_support_overrides(RID p_font_rid) = 0;
virtual bool font_is_script_supported(RID p_font_rid, const String &p_script) const = 0;
virtual void font_set_script_support_override(RID p_font_rid, const String &p_script, bool p_supported) = 0;
virtual bool font_get_script_support_override(RID p_font_rid, const String &p_script) = 0;
virtual void font_remove_script_support_override(RID p_font_rid, const String &p_script) = 0;
virtual Vector<String> font_get_script_support_overrides(RID p_font_rid) = 0;
virtual Dictionary font_supported_feature_list(RID p_font_rid) const = 0;
virtual Dictionary font_supported_variation_list(RID p_font_rid) const = 0;
virtual real_t font_get_global_oversampling() const = 0;
virtual void font_set_global_oversampling(real_t p_oversampling) = 0;
Vector2 get_hex_code_box_size(int p_size, char32_t p_index) const;
void draw_hex_code_box(RID p_canvas, int p_size, const Vector2 &p_pos, char32_t p_index, const Color &p_color) const;
virtual Vector<String> get_system_fonts() const = 0;
/* Shaped text buffer interface */
virtual RID create_shaped_text(Direction p_direction = DIRECTION_AUTO, Orientation p_orientation = ORIENTATION_HORIZONTAL) = 0;
@ -351,8 +409,8 @@ public:
virtual RID shaped_text_substr(RID p_shaped, int p_start, int p_length) const = 0; // Copy shaped substring (e.g. line break) without reshaping, but correctly reordered, preservers range.
virtual RID shaped_text_get_parent(RID p_shaped) const = 0;
virtual float shaped_text_fit_to_width(RID p_shaped, float p_width, uint8_t /*JustificationFlag*/ p_jst_flags = JUSTIFICATION_WORD_BOUND | JUSTIFICATION_KASHIDA) = 0;
virtual float shaped_text_tab_align(RID p_shaped, const Vector<float> &p_tab_stops) = 0;
virtual real_t shaped_text_fit_to_width(RID p_shaped, real_t p_width, uint8_t /*JustificationFlag*/ p_jst_flags = JUSTIFICATION_WORD_BOUND | JUSTIFICATION_KASHIDA) = 0;
virtual real_t shaped_text_tab_align(RID p_shaped, const Vector<real_t> &p_tab_stops) = 0;
virtual bool shaped_text_shape(RID p_shaped) = 0;
virtual bool shaped_text_update_breaks(RID p_shaped) = 0;
@ -366,36 +424,36 @@ public:
virtual Vector<Glyph> shaped_text_sort_logical(RID p_shaped) = 0;
virtual Vector<Vector2i> shaped_text_get_line_breaks_adv(RID p_shaped, const Vector<float> &p_width, int p_start = 0, bool p_once = true, uint8_t /*TextBreakFlag*/ p_break_flags = BREAK_MANDATORY | BREAK_WORD_BOUND) const;
virtual Vector<Vector2i> shaped_text_get_line_breaks(RID p_shaped, float p_width, int p_start = 0, uint8_t /*TextBreakFlag*/ p_break_flags = BREAK_MANDATORY | BREAK_WORD_BOUND) const;
virtual Vector<Vector2i> shaped_text_get_line_breaks_adv(RID p_shaped, const Vector<real_t> &p_width, int p_start = 0, bool p_once = true, uint8_t /*TextBreakFlag*/ p_break_flags = BREAK_MANDATORY | BREAK_WORD_BOUND) const;
virtual Vector<Vector2i> shaped_text_get_line_breaks(RID p_shaped, real_t p_width, int p_start = 0, uint8_t /*TextBreakFlag*/ p_break_flags = BREAK_MANDATORY | BREAK_WORD_BOUND) const;
virtual Vector<Vector2i> shaped_text_get_word_breaks(RID p_shaped, int p_grapheme_flags = GRAPHEME_IS_SPACE | GRAPHEME_IS_PUNCTUATION) const;
virtual TrimData shaped_text_get_trim_data(RID p_shaped) const;
virtual void shaped_text_overrun_trim_to_width(RID p_shaped, float p_width, uint8_t p_trim_flags) = 0;
virtual void shaped_text_overrun_trim_to_width(RID p_shaped, real_t p_width, uint8_t p_trim_flags) = 0;
virtual Array shaped_text_get_objects(RID p_shaped) const = 0;
virtual Rect2 shaped_text_get_object_rect(RID p_shaped, Variant p_key) const = 0;
virtual Size2 shaped_text_get_size(RID p_shaped) const = 0;
virtual float shaped_text_get_ascent(RID p_shaped) const = 0;
virtual float shaped_text_get_descent(RID p_shaped) const = 0;
virtual float shaped_text_get_width(RID p_shaped) const = 0;
virtual float shaped_text_get_underline_position(RID p_shaped) const = 0;
virtual float shaped_text_get_underline_thickness(RID p_shaped) const = 0;
virtual real_t shaped_text_get_ascent(RID p_shaped) const = 0;
virtual real_t shaped_text_get_descent(RID p_shaped) const = 0;
virtual real_t shaped_text_get_width(RID p_shaped) const = 0;
virtual real_t shaped_text_get_underline_position(RID p_shaped) const = 0;
virtual real_t shaped_text_get_underline_thickness(RID p_shaped) const = 0;
virtual Direction shaped_text_get_dominant_direciton_in_range(RID p_shaped, int p_start, int p_end) const;
virtual void shaped_text_get_carets(RID p_shaped, int p_position, Rect2 &p_leading_caret, Direction &p_leading_dir, Rect2 &p_trailing_caret, Direction &p_trailing_dir) const;
virtual Vector<Vector2> shaped_text_get_selection(RID p_shaped, int p_start, int p_end) const;
virtual int shaped_text_hit_test_grapheme(RID p_shaped, float p_coords) const; // Return grapheme index.
virtual int shaped_text_hit_test_position(RID p_shaped, float p_coords) const; // Return caret/selection position.
virtual int shaped_text_hit_test_grapheme(RID p_shaped, real_t p_coords) const; // Return grapheme index.
virtual int shaped_text_hit_test_position(RID p_shaped, real_t p_coords) const; // Return caret/selection position.
virtual int shaped_text_next_grapheme_pos(RID p_shaped, int p_pos);
virtual int shaped_text_prev_grapheme_pos(RID p_shaped, int p_pos);
// The pen position is always placed on the baseline and moveing left to right.
virtual void shaped_text_draw(RID p_shaped, RID p_canvas, const Vector2 &p_pos, float p_clip_l = -1.f, float p_clip_r = -1.f, const Color &p_color = Color(1, 1, 1)) const;
virtual void shaped_text_draw_outline(RID p_shaped, RID p_canvas, const Vector2 &p_pos, float p_clip_l = -1.f, float p_clip_r = -1.f, int p_outline_size = 1, const Color &p_color = Color(1, 1, 1)) const;
virtual void shaped_text_draw(RID p_shaped, RID p_canvas, const Vector2 &p_pos, real_t p_clip_l = -1.f, real_t p_clip_r = -1.f, const Color &p_color = Color(1, 1, 1)) const;
virtual void shaped_text_draw_outline(RID p_shaped, RID p_canvas, const Vector2 &p_pos, real_t p_clip_l = -1.f, real_t p_clip_r = -1.f, int p_outline_size = 1, const Color &p_color = Color(1, 1, 1)) const;
// Number conversion.
virtual String format_number(const String &p_string, const String &p_language = "") const { return p_string; };
@ -403,9 +461,9 @@ public:
virtual String percent_sign(const String &p_language = "") const { return "%"; };
/* GDScript wrappers */
RID _create_font_memory(const PackedByteArray &p_data, const String &p_type, int p_base_size = 16);
RID _create_font_memory(const PackedByteArray &p_data, int p_base_size = 16);
Dictionary _font_get_glyph_contours(RID p_font, int p_size, uint32_t p_index) const;
Dictionary _font_get_glyph_contours(RID p_font, int p_size, int32_t p_index) const;
Array _shaped_text_get_glyphs(RID p_shaped) const;
Dictionary _shaped_text_get_carets(RID p_shaped, int p_position) const;
@ -413,7 +471,7 @@ public:
void _shaped_text_set_bidi_override(RID p_shaped, const Array &p_override);
Array _shaped_text_get_line_breaks_adv(RID p_shaped, const PackedFloat32Array &p_width, int p_start, bool p_once, uint8_t p_break_flags) const;
Array _shaped_text_get_line_breaks(RID p_shaped, float p_width, int p_start, uint8_t p_break_flags) const;
Array _shaped_text_get_line_breaks(RID p_shaped, real_t p_width, int p_start, uint8_t p_break_flags) const;
Array _shaped_text_get_word_breaks(RID p_shaped) const;
Array _shaped_text_get_selection(RID p_shaped, int p_start, int p_end) const;
@ -491,5 +549,6 @@ VARIANT_ENUM_CAST(TextServer::GraphemeFlag);
VARIANT_ENUM_CAST(TextServer::Hinting);
VARIANT_ENUM_CAST(TextServer::Feature);
VARIANT_ENUM_CAST(TextServer::ContourPointTag);
VARIANT_ENUM_CAST(TextServer::SpacingType);
#endif // TEXT_SERVER_H

View File

@ -55,7 +55,8 @@ TEST_SUITE("[[TextServer]") {
for (int i = 0; i < TextServerManager::get_interface_count(); i++) {
TextServer *ts = TextServerManager::initialize(i, err);
RID font = ts->create_font_memory(_font_NotoSans_Regular, _font_NotoSans_Regular_size, "ttf");
RID font = ts->create_font();
ts->font_set_data_ptr(font, _font_NotoSans_Regular, _font_NotoSans_Regular_size);
TEST_FAIL_COND(font == RID(), "Loading font failed.");
ts->free(font);
}
@ -65,9 +66,14 @@ TEST_SUITE("[[TextServer]") {
for (int i = 0; i < TextServerManager::get_interface_count(); i++) {
TextServer *ts = TextServerManager::initialize(i, err);
RID font1 = ts->create_font();
ts->font_set_data_ptr(font1, _font_NotoSans_Regular, _font_NotoSans_Regular_size);
RID font2 = ts->create_font();
ts->font_set_data_ptr(font2, _font_NotoSansThaiUI_Regular, _font_NotoSansThaiUI_Regular_size);
Vector<RID> font;
font.push_back(ts->create_font_memory(_font_NotoSans_Regular, _font_NotoSans_Regular_size, "ttf"));
font.push_back(ts->create_font_memory(_font_NotoSansThaiUI_Regular, _font_NotoSansThaiUI_Regular_size, "ttf"));
font.push_back(font1);
font.push_back(font2);
String test = U"คนอ้วน khon uan ראה";
// 6^ 17^
@ -111,9 +117,14 @@ TEST_SUITE("[[TextServer]") {
continue;
}
RID font1 = ts->create_font();
ts->font_set_data_ptr(font1, _font_NotoSans_Regular, _font_NotoSans_Regular_size);
RID font2 = ts->create_font();
ts->font_set_data_ptr(font2, _font_NotoNaskhArabicUI_Regular, _font_NotoNaskhArabicUI_Regular_size);
Vector<RID> font;
font.push_back(ts->create_font_memory(_font_NotoSans_Regular, _font_NotoSans_Regular_size, "ttf"));
font.push_back(ts->create_font_memory(_font_NotoNaskhArabicUI_Regular, _font_NotoNaskhArabicUI_Regular_size, "ttf"));
font.push_back(font1);
font.push_back(font2);
String test = U"Arabic (اَلْعَرَبِيَّةُ, al-ʿarabiyyah)";
// 7^ 26^
@ -155,9 +166,14 @@ TEST_SUITE("[[TextServer]") {
String test_1 = U"test test test";
// 5^ 10^
RID font1 = ts->create_font();
ts->font_set_data_ptr(font1, _font_NotoSans_Regular, _font_NotoSans_Regular_size);
RID font2 = ts->create_font();
ts->font_set_data_ptr(font2, _font_NotoSansThaiUI_Regular, _font_NotoSansThaiUI_Regular_size);
Vector<RID> font;
font.push_back(ts->create_font_memory(_font_NotoSans_Regular, _font_NotoSans_Regular_size, "ttf"));
font.push_back(ts->create_font_memory(_font_NotoSansThaiUI_Regular, _font_NotoSansThaiUI_Regular_size, "ttf"));
font.push_back(font1);
font.push_back(font2);
RID ctx = ts->create_shaped_text();
TEST_FAIL_COND(ctx == RID(), "Creating text buffer failed.");
@ -185,9 +201,14 @@ TEST_SUITE("[[TextServer]") {
for (int i = 0; i < TextServerManager::get_interface_count(); i++) {
TextServer *ts = TextServerManager::initialize(i, err);
RID font1 = ts->create_font();
ts->font_set_data_ptr(font1, _font_NotoSans_Regular, _font_NotoSans_Regular_size);
RID font2 = ts->create_font();
ts->font_set_data_ptr(font2, _font_NotoNaskhArabicUI_Regular, _font_NotoNaskhArabicUI_Regular_size);
Vector<RID> font;
font.push_back(ts->create_font_memory(_font_NotoSans_Regular, _font_NotoSans_Regular_size, "ttf"));
font.push_back(ts->create_font_memory(_font_NotoNaskhArabicUI_Regular, _font_NotoNaskhArabicUI_Regular_size, "ttf"));
font.push_back(font1);
font.push_back(font2);
String test_1 = U"الحمد";
String test_2 = U"الحمد test";
@ -242,7 +263,6 @@ TEST_SUITE("[[TextServer]") {
font.clear();
}
}
memdelete(tsman);
}
}

13
thirdparty/README.md vendored
View File

@ -482,6 +482,19 @@ Collection of single-file libraries used in Godot components.
* License: BSD
## msdfgen
- Upstream: https://github.com/Chlumsky/msdfgen
- Version: 1.9.1 (1b3b6b985094e6f12751177490add3ad11dd91a9, 2010)
- License: MIT
Files extracted from the upstream source:
- `msdfgen.h`
- Files in `core/` folder.
- `LICENSE.txt` and `CHANGELOG.md`
## nanosvg
- Upstream: https://github.com/memononen/nanosvg

82
thirdparty/msdfgen/CHANGELOG.md vendored Normal file
View File

@ -0,0 +1,82 @@
## Version 1.9 (2021-05-28)
- Error correction of multi-channel distance fields has been completely reworked
- Added new edge coloring strategy that optimizes colors based on distances between edges
- Added some minor functions for the library API
- Minor code refactor and optimizations
## Version 1.8 (2020-10-17)
- Integrated the Skia library into the project, which is used to preprocess the shape geometry and eliminate any self-intersections and other irregularities previously unsupported by the software
- The scanline pass and overlapping contour mode is made obsolete by this step and has been disabled by default. The preprocess step can be disabled by the new `-nopreprocess` switch and the former enabled by `-scanline` and `-overlap` respectively.
- The project can be built without the Skia library, forgoing the geometry preprocessing feature. This is controlled by the macro definition `MSDFGEN_USE_SKIA`
- Significantly improved performance of the core algorithm by reusing results from previously computed pixels
- Introduced an additional error correction routine which eliminates MSDF artifacts by analytically predicting results of bilinear interpolation
- Added the possibility to load font glyphs by their index rather than a Unicode value (use the prefix `g` before the character code in `-font` argument)
- Added `-distanceshift` argument that can be used to adjust the center of the distance range in the output distance field
- Fixed several errors in the evaluation of curve distances
- Fixed an issue with paths containing convergent corners (those whose inner angle is zero)
- The algorithm for pseudo-distance computation slightly changed, fixing certain rare edge cases and improving consistency
- Added the ability to supply own `FT_Face` handle to the msdfgen library
- Minor refactor of the core algorithm
### Version 1.7.1 (2020-03-09)
- Fixed an edge case bug in scanline rasterization
## Version 1.7 (2020-03-07)
- Added `mtsdf` mode - a combination of `msdf` with `sdf` in the alpha channel
- Distance fields can now be stored as uncompressed TIFF image files with floating point precision
- Bitmap class refactor - template argument split into data type and number of channels, bitmap reference classes introduced
- Added a secondary "ink trap" edge coloring heuristic, can be selected using `-coloringstrategy inktrap`
- Added computation of estimated rendering error for a given SDF
- Added computation of bounding box that includes sharp mitered corners
- The API for bounds computation of the `Shape` class changed for clarity
- Fixed several edge case bugs
## Version 1.6 (2019-04-08)
- Core algorithm rewritten to split up advanced edge selection logic into modular template arguments.
- Pseudo-distance evaluation reworked to eliminate discontinuities at the midpoint between edges.
- MSDF error correction reworked to also fix distances away from edges and consider diagonal pairs. Code simplified.
- Added scanline rasterization support for `Shape`.
- Added a scanline pass in the standalone version, which corrects the signs in the distance field according to the selected fill rule (`-fillrule`). Can be disabled using `-noscanline`.
- Fixed autoframe scaling, which previously caused the output to have unnecessary empty border.
- `-guessorder` switch no longer enabled by default, as the functionality is now provided by the scanline pass.
- Updated FreeType and other libraries, changed to static linkage
- Added 64-bit and static library builds to the Visual Studio solution
## Version 1.5 (2017-07-23)
- Fixed rounding error in cubic curve splitting.
- SVG parser fixes and support for additional path commands.
- Added CMake build script.
## Version 1.4 (2017-02-09)
- Reworked contour combining logic to support overlapping contours. Original algorithm preserved in functions with `_legacy` suffix, which are invoked by the new `-legacy` switch.
- Fixed a severe bug in cubic curve distance computation, where a control point lies at the endpoint.
- Standalone version now automatically detects if the input has the wrong orientation and adjusts the distance field accordingly. Can be disabled by `-keeporder` or `-reverseorder` switch.
- SVG parser fixes and improvements.
## Version 1.3 (2016-12-07)
- Fixed `-reverseorder` switch.
- Fixed glyph loading to use the proper method of acquiring outlines from FreeType.
## Version 1.2 (2016-07-20)
- Added option to specify that shape vertices are listed in reverse order (`-reverseorder`).
- Added option to set a seed for the edge coloring heuristic (-seed \<n\>), which can be used to adjust the output.
- Fixed parsing of glyph contours that start with a curve control point.
## Version 1.1 (2016-05-08)
- Switched to MIT license due to popular demand.
- Fixed SDF rendering anti-aliasing when the output is smaller than the distance field.
## Version 1.0 (2016-04-28)
- Project published.

21
thirdparty/msdfgen/LICENSE.txt vendored Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2016 Viktor Chlumsky
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.

50
thirdparty/msdfgen/core/Bitmap.h vendored Normal file
View File

@ -0,0 +1,50 @@
#pragma once
#include "BitmapRef.hpp"
namespace msdfgen {
/// A 2D image bitmap with N channels of type T. Pixel memory is managed by the class.
template <typename T, int N = 1>
class Bitmap {
public:
Bitmap();
Bitmap(int width, int height);
Bitmap(const BitmapConstRef<T, N> &orig);
Bitmap(const Bitmap<T, N> &orig);
#ifdef MSDFGEN_USE_CPP11
Bitmap(Bitmap<T, N> &&orig);
#endif
~Bitmap();
Bitmap<T, N> & operator=(const BitmapConstRef<T, N> &orig);
Bitmap<T, N> & operator=(const Bitmap<T, N> &orig);
#ifdef MSDFGEN_USE_CPP11
Bitmap<T, N> & operator=(Bitmap<T, N> &&orig);
#endif
/// Bitmap width in pixels.
int width() const;
/// Bitmap height in pixels.
int height() const;
T * operator()(int x, int y);
const T * operator()(int x, int y) const;
#ifdef MSDFGEN_USE_CPP11
explicit operator T *();
explicit operator const T *() const;
#else
operator T *();
operator const T *() const;
#endif
operator BitmapRef<T, N>();
operator BitmapConstRef<T, N>() const;
private:
T *pixels;
int w, h;
};
}
#include "Bitmap.hpp"

117
thirdparty/msdfgen/core/Bitmap.hpp vendored Normal file
View File

@ -0,0 +1,117 @@
#include "Bitmap.h"
#include <cstdlib>
#include <cstring>
namespace msdfgen {
template <typename T, int N>
Bitmap<T, N>::Bitmap() : pixels(NULL), w(0), h(0) { }
template <typename T, int N>
Bitmap<T, N>::Bitmap(int width, int height) : w(width), h(height) {
pixels = new T[N*w*h];
}
template <typename T, int N>
Bitmap<T, N>::Bitmap(const BitmapConstRef<T, N> &orig) : w(orig.width), h(orig.height) {
pixels = new T[N*w*h];
memcpy(pixels, orig.pixels, sizeof(T)*N*w*h);
}
template <typename T, int N>
Bitmap<T, N>::Bitmap(const Bitmap<T, N> &orig) : w(orig.w), h(orig.h) {
pixels = new T[N*w*h];
memcpy(pixels, orig.pixels, sizeof(T)*N*w*h);
}
#ifdef MSDFGEN_USE_CPP11
template <typename T, int N>
Bitmap<T, N>::Bitmap(Bitmap<T, N> &&orig) : pixels(orig.pixels), w(orig.w), h(orig.h) {
orig.pixels = NULL;
orig.w = 0, orig.h = 0;
}
#endif
template <typename T, int N>
Bitmap<T, N>::~Bitmap() {
delete [] pixels;
}
template <typename T, int N>
Bitmap<T, N> & Bitmap<T, N>::operator=(const BitmapConstRef<T, N> &orig) {
if (pixels != orig.pixels) {
delete [] pixels;
w = orig.width, h = orig.height;
pixels = new T[N*w*h];
memcpy(pixels, orig.pixels, sizeof(T)*N*w*h);
}
return *this;
}
template <typename T, int N>
Bitmap<T, N> & Bitmap<T, N>::operator=(const Bitmap<T, N> &orig) {
if (this != &orig) {
delete [] pixels;
w = orig.w, h = orig.h;
pixels = new T[N*w*h];
memcpy(pixels, orig.pixels, sizeof(T)*N*w*h);
}
return *this;
}
#ifdef MSDFGEN_USE_CPP11
template <typename T, int N>
Bitmap<T, N> & Bitmap<T, N>::operator=(Bitmap<T, N> &&orig) {
if (this != &orig) {
delete [] pixels;
pixels = orig.pixels;
w = orig.w, h = orig.h;
orig.pixels = NULL;
}
return *this;
}
#endif
template <typename T, int N>
int Bitmap<T, N>::width() const {
return w;
}
template <typename T, int N>
int Bitmap<T, N>::height() const {
return h;
}
template <typename T, int N>
T * Bitmap<T, N>::operator()(int x, int y) {
return pixels+N*(w*y+x);
}
template <typename T, int N>
const T * Bitmap<T, N>::operator()(int x, int y) const {
return pixels+N*(w*y+x);
}
template <typename T, int N>
Bitmap<T, N>::operator T *() {
return pixels;
}
template <typename T, int N>
Bitmap<T, N>::operator const T *() const {
return pixels;
}
template <typename T, int N>
Bitmap<T, N>::operator BitmapRef<T, N>() {
return BitmapRef<T, N>(pixels, w, h);
}
template <typename T, int N>
Bitmap<T, N>::operator BitmapConstRef<T, N>() const {
return BitmapConstRef<T, N>(pixels, w, h);
}
}

43
thirdparty/msdfgen/core/BitmapRef.hpp vendored Normal file
View File

@ -0,0 +1,43 @@
#pragma once
#include <cstdlib>
namespace msdfgen {
typedef unsigned char byte;
/// Reference to a 2D image bitmap or a buffer acting as one. Pixel storage not owned or managed by the object.
template <typename T, int N = 1>
struct BitmapRef {
T *pixels;
int width, height;
inline BitmapRef() : pixels(NULL), width(0), height(0) { }
inline BitmapRef(T *pixels, int width, int height) : pixels(pixels), width(width), height(height) { }
inline T * operator()(int x, int y) const {
return pixels+N*(width*y+x);
}
};
/// Constant reference to a 2D image bitmap or a buffer acting as one. Pixel storage not owned or managed by the object.
template <typename T, int N = 1>
struct BitmapConstRef {
const T *pixels;
int width, height;
inline BitmapConstRef() : pixels(NULL), width(0), height(0) { }
inline BitmapConstRef(const T *pixels, int width, int height) : pixels(pixels), width(width), height(height) { }
inline BitmapConstRef(const BitmapRef<T, N> &orig) : pixels(orig.pixels), width(orig.width), height(orig.height) { }
inline const T * operator()(int x, int y) const {
return pixels+N*(width*y+x);
}
};
}

90
thirdparty/msdfgen/core/Contour.cpp vendored Normal file
View File

@ -0,0 +1,90 @@
#include "Contour.h"
#include "arithmetics.hpp"
namespace msdfgen {
static double shoelace(const Point2 &a, const Point2 &b) {
return (b.x-a.x)*(a.y+b.y);
}
void Contour::addEdge(const EdgeHolder &edge) {
edges.push_back(edge);
}
#ifdef MSDFGEN_USE_CPP11
void Contour::addEdge(EdgeHolder &&edge) {
edges.push_back((EdgeHolder &&) edge);
}
#endif
EdgeHolder & Contour::addEdge() {
edges.resize(edges.size()+1);
return edges.back();
}
static void boundPoint(double &l, double &b, double &r, double &t, Point2 p) {
if (p.x < l) l = p.x;
if (p.y < b) b = p.y;
if (p.x > r) r = p.x;
if (p.y > t) t = p.y;
}
void Contour::bound(double &l, double &b, double &r, double &t) const {
for (std::vector<EdgeHolder>::const_iterator edge = edges.begin(); edge != edges.end(); ++edge)
(*edge)->bound(l, b, r, t);
}
void Contour::boundMiters(double &l, double &b, double &r, double &t, double border, double miterLimit, int polarity) const {
if (edges.empty())
return;
Vector2 prevDir = edges.back()->direction(1).normalize(true);
for (std::vector<EdgeHolder>::const_iterator edge = edges.begin(); edge != edges.end(); ++edge) {
Vector2 dir = -(*edge)->direction(0).normalize(true);
if (polarity*crossProduct(prevDir, dir) >= 0) {
double miterLength = miterLimit;
double q = .5*(1-dotProduct(prevDir, dir));
if (q > 0)
miterLength = min(1/sqrt(q), miterLimit);
Point2 miter = (*edge)->point(0)+border*miterLength*(prevDir+dir).normalize(true);
boundPoint(l, b, r, t, miter);
}
prevDir = (*edge)->direction(1).normalize(true);
}
}
int Contour::winding() const {
if (edges.empty())
return 0;
double total = 0;
if (edges.size() == 1) {
Point2 a = edges[0]->point(0), b = edges[0]->point(1/3.), c = edges[0]->point(2/3.);
total += shoelace(a, b);
total += shoelace(b, c);
total += shoelace(c, a);
} else if (edges.size() == 2) {
Point2 a = edges[0]->point(0), b = edges[0]->point(.5), c = edges[1]->point(0), d = edges[1]->point(.5);
total += shoelace(a, b);
total += shoelace(b, c);
total += shoelace(c, d);
total += shoelace(d, a);
} else {
Point2 prev = edges.back()->point(0);
for (std::vector<EdgeHolder>::const_iterator edge = edges.begin(); edge != edges.end(); ++edge) {
Point2 cur = (*edge)->point(0);
total += shoelace(prev, cur);
prev = cur;
}
}
return sign(total);
}
void Contour::reverse() {
for (int i = (int) edges.size()/2; i > 0; --i)
EdgeHolder::swap(edges[i-1], edges[edges.size()-i]);
for (std::vector<EdgeHolder>::iterator edge = edges.begin(); edge != edges.end(); ++edge)
(*edge)->reverse();
}
}

34
thirdparty/msdfgen/core/Contour.h vendored Normal file
View File

@ -0,0 +1,34 @@
#pragma once
#include <vector>
#include "EdgeHolder.h"
namespace msdfgen {
/// A single closed contour of a shape.
class Contour {
public:
/// The sequence of edges that make up the contour.
std::vector<EdgeHolder> edges;
/// Adds an edge to the contour.
void addEdge(const EdgeHolder &edge);
#ifdef MSDFGEN_USE_CPP11
void addEdge(EdgeHolder &&edge);
#endif
/// Creates a new edge in the contour and returns its reference.
EdgeHolder & addEdge();
/// Adjusts the bounding box to fit the contour.
void bound(double &l, double &b, double &r, double &t) const;
/// Adjusts the bounding box to fit the contour border's mitered corners.
void boundMiters(double &l, double &b, double &r, double &t, double border, double miterLimit, int polarity) const;
/// Computes the winding of the contour. Returns 1 if positive, -1 if negative.
int winding() const;
/// Reverses the sequence of edges on the contour.
void reverse();
};
}

18
thirdparty/msdfgen/core/EdgeColor.h vendored Normal file
View File

@ -0,0 +1,18 @@
#pragma once
namespace msdfgen {
/// Edge color specifies which color channels an edge belongs to.
enum EdgeColor {
BLACK = 0,
RED = 1,
GREEN = 2,
YELLOW = 3,
BLUE = 4,
MAGENTA = 5,
CYAN = 6,
WHITE = 7
};
}

77
thirdparty/msdfgen/core/EdgeHolder.cpp vendored Normal file
View File

@ -0,0 +1,77 @@
#include "EdgeHolder.h"
namespace msdfgen {
void EdgeHolder::swap(EdgeHolder &a, EdgeHolder &b) {
EdgeSegment *tmp = a.edgeSegment;
a.edgeSegment = b.edgeSegment;
b.edgeSegment = tmp;
}
EdgeHolder::EdgeHolder() : edgeSegment(NULL) { }
EdgeHolder::EdgeHolder(EdgeSegment *segment) : edgeSegment(segment) { }
EdgeHolder::EdgeHolder(Point2 p0, Point2 p1, EdgeColor edgeColor) : edgeSegment(new LinearSegment(p0, p1, edgeColor)) { }
EdgeHolder::EdgeHolder(Point2 p0, Point2 p1, Point2 p2, EdgeColor edgeColor) : edgeSegment(new QuadraticSegment(p0, p1, p2, edgeColor)) { }
EdgeHolder::EdgeHolder(Point2 p0, Point2 p1, Point2 p2, Point2 p3, EdgeColor edgeColor) : edgeSegment(new CubicSegment(p0, p1, p2, p3, edgeColor)) { }
EdgeHolder::EdgeHolder(const EdgeHolder &orig) : edgeSegment(orig.edgeSegment ? orig.edgeSegment->clone() : NULL) { }
#ifdef MSDFGEN_USE_CPP11
EdgeHolder::EdgeHolder(EdgeHolder &&orig) : edgeSegment(orig.edgeSegment) {
orig.edgeSegment = NULL;
}
#endif
EdgeHolder::~EdgeHolder() {
delete edgeSegment;
}
EdgeHolder & EdgeHolder::operator=(const EdgeHolder &orig) {
if (this != &orig) {
delete edgeSegment;
edgeSegment = orig.edgeSegment ? orig.edgeSegment->clone() : NULL;
}
return *this;
}
#ifdef MSDFGEN_USE_CPP11
EdgeHolder & EdgeHolder::operator=(EdgeHolder &&orig) {
if (this != &orig) {
delete edgeSegment;
edgeSegment = orig.edgeSegment;
orig.edgeSegment = NULL;
}
return *this;
}
#endif
EdgeSegment & EdgeHolder::operator*() {
return *edgeSegment;
}
const EdgeSegment & EdgeHolder::operator*() const {
return *edgeSegment;
}
EdgeSegment * EdgeHolder::operator->() {
return edgeSegment;
}
const EdgeSegment * EdgeHolder::operator->() const {
return edgeSegment;
}
EdgeHolder::operator EdgeSegment *() {
return edgeSegment;
}
EdgeHolder::operator const EdgeSegment *() const {
return edgeSegment;
}
}

41
thirdparty/msdfgen/core/EdgeHolder.h vendored Normal file
View File

@ -0,0 +1,41 @@
#pragma once
#include "edge-segments.h"
namespace msdfgen {
/// Container for a single edge of dynamic type.
class EdgeHolder {
public:
/// Swaps the edges held by a and b.
static void swap(EdgeHolder &a, EdgeHolder &b);
EdgeHolder();
EdgeHolder(EdgeSegment *segment);
EdgeHolder(Point2 p0, Point2 p1, EdgeColor edgeColor = WHITE);
EdgeHolder(Point2 p0, Point2 p1, Point2 p2, EdgeColor edgeColor = WHITE);
EdgeHolder(Point2 p0, Point2 p1, Point2 p2, Point2 p3, EdgeColor edgeColor = WHITE);
EdgeHolder(const EdgeHolder &orig);
#ifdef MSDFGEN_USE_CPP11
EdgeHolder(EdgeHolder &&orig);
#endif
~EdgeHolder();
EdgeHolder & operator=(const EdgeHolder &orig);
#ifdef MSDFGEN_USE_CPP11
EdgeHolder & operator=(EdgeHolder &&orig);
#endif
EdgeSegment & operator*();
const EdgeSegment & operator*() const;
EdgeSegment * operator->();
const EdgeSegment * operator->() const;
operator EdgeSegment *();
operator const EdgeSegment *() const;
private:
EdgeSegment *edgeSegment;
};
}

View File

@ -0,0 +1,495 @@
#include "MSDFErrorCorrection.h"
#include <cstring>
#include "arithmetics.hpp"
#include "equation-solver.h"
#include "EdgeColor.h"
#include "bitmap-interpolation.hpp"
#include "edge-selectors.h"
#include "contour-combiners.h"
#include "ShapeDistanceFinder.h"
#include "generator-config.h"
namespace msdfgen {
#define ARTIFACT_T_EPSILON .01
#define PROTECTION_RADIUS_TOLERANCE 1.001
#define CLASSIFIER_FLAG_CANDIDATE 0x01
#define CLASSIFIER_FLAG_ARTIFACT 0x02
const double ErrorCorrectionConfig::defaultMinDeviationRatio = 1.11111111111111111;
const double ErrorCorrectionConfig::defaultMinImproveRatio = 1.11111111111111111;
/// The base artifact classifier recognizes artifacts based on the contents of the SDF alone.
class BaseArtifactClassifier {
public:
inline BaseArtifactClassifier(double span, bool protectedFlag) : span(span), protectedFlag(protectedFlag) { }
/// Evaluates if the median value xm interpolated at xt in the range between am at at and bm at bt indicates an artifact.
inline int rangeTest(double at, double bt, double xt, float am, float bm, float xm) const {
// For protected texels, only consider inversion artifacts (interpolated median has different sign than boundaries). For the rest, it is sufficient that the interpolated median is outside its boundaries.
if ((am > .5f && bm > .5f && xm <= .5f) || (am < .5f && bm < .5f && xm >= .5f) || (!protectedFlag && median(am, bm, xm) != xm)) {
double axSpan = (xt-at)*span, bxSpan = (bt-xt)*span;
// Check if the interpolated median's value is in the expected range based on its distance (span) from boundaries a, b.
if (!(xm >= am-axSpan && xm <= am+axSpan && xm >= bm-bxSpan && xm <= bm+bxSpan))
return CLASSIFIER_FLAG_CANDIDATE|CLASSIFIER_FLAG_ARTIFACT;
return CLASSIFIER_FLAG_CANDIDATE;
}
return 0;
}
/// Returns true if the combined results of the tests performed on the median value m interpolated at t indicate an artifact.
inline bool evaluate(double t, float m, int flags) const {
return (flags&2) != 0;
}
private:
double span;
bool protectedFlag;
};
/// The shape distance checker evaluates the exact shape distance to find additional artifacts at a significant performance cost.
template <template <typename> class ContourCombiner, int N>
class ShapeDistanceChecker {
public:
class ArtifactClassifier : public BaseArtifactClassifier {
public:
inline ArtifactClassifier(ShapeDistanceChecker *parent, const Vector2 &direction, double span) : BaseArtifactClassifier(span, parent->protectedFlag), parent(parent), direction(direction) { }
/// Returns true if the combined results of the tests performed on the median value m interpolated at t indicate an artifact.
inline bool evaluate(double t, float m, int flags) const {
if (flags&CLASSIFIER_FLAG_CANDIDATE) {
// Skip expensive distance evaluation if the point has already been classified as an artifact by the base classifier.
if (flags&CLASSIFIER_FLAG_ARTIFACT)
return true;
Vector2 tVector = t*direction;
float oldMSD[N], newMSD[3];
// Compute the color that would be currently interpolated at the artifact candidate's position.
Point2 sdfCoord = parent->sdfCoord+tVector;
interpolate(oldMSD, parent->sdf, sdfCoord);
// Compute the color that would be interpolated at the artifact candidate's position if error correction was applied on the current texel.
double aWeight = (1-fabs(tVector.x))*(1-fabs(tVector.y));
float aPSD = median(parent->msd[0], parent->msd[1], parent->msd[2]);
newMSD[0] = float(oldMSD[0]+aWeight*(aPSD-parent->msd[0]));
newMSD[1] = float(oldMSD[1]+aWeight*(aPSD-parent->msd[1]));
newMSD[2] = float(oldMSD[2]+aWeight*(aPSD-parent->msd[2]));
// Compute the evaluated distance (interpolated median) before and after error correction, as well as the exact shape distance.
float oldPSD = median(oldMSD[0], oldMSD[1], oldMSD[2]);
float newPSD = median(newMSD[0], newMSD[1], newMSD[2]);
float refPSD = float(parent->invRange*parent->distanceFinder.distance(parent->shapeCoord+tVector*parent->texelSize)+.5);
// Compare the differences of the exact distance and the before and after distances.
return parent->minImproveRatio*fabsf(newPSD-refPSD) < double(fabsf(oldPSD-refPSD));
}
return false;
}
private:
ShapeDistanceChecker *parent;
Vector2 direction;
};
Point2 shapeCoord, sdfCoord;
const float *msd;
bool protectedFlag;
inline ShapeDistanceChecker(const BitmapConstRef<float, N> &sdf, const Shape &shape, const Projection &projection, double invRange, double minImproveRatio) : distanceFinder(shape), sdf(sdf), invRange(invRange), minImproveRatio(minImproveRatio) {
texelSize = projection.unprojectVector(Vector2(1));
}
inline ArtifactClassifier classifier(const Vector2 &direction, double span) {
return ArtifactClassifier(this, direction, span);
}
private:
ShapeDistanceFinder<ContourCombiner<PseudoDistanceSelector> > distanceFinder;
BitmapConstRef<float, N> sdf;
double invRange;
Vector2 texelSize;
double minImproveRatio;
};
MSDFErrorCorrection::MSDFErrorCorrection() { }
MSDFErrorCorrection::MSDFErrorCorrection(const BitmapRef<byte, 1> &stencil, const Projection &projection, double range) : stencil(stencil), projection(projection) {
invRange = 1/range;
minDeviationRatio = ErrorCorrectionConfig::defaultMinDeviationRatio;
minImproveRatio = ErrorCorrectionConfig::defaultMinImproveRatio;
memset(stencil.pixels, 0, sizeof(byte)*stencil.width*stencil.height);
}
void MSDFErrorCorrection::setMinDeviationRatio(double minDeviationRatio) {
this->minDeviationRatio = minDeviationRatio;
}
void MSDFErrorCorrection::setMinImproveRatio(double minImproveRatio) {
this->minImproveRatio = minImproveRatio;
}
void MSDFErrorCorrection::protectCorners(const Shape &shape) {
for (std::vector<Contour>::const_iterator contour = shape.contours.begin(); contour != shape.contours.end(); ++contour)
if (!contour->edges.empty()) {
const EdgeSegment *prevEdge = contour->edges.back();
for (std::vector<EdgeHolder>::const_iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge) {
int commonColor = prevEdge->color&(*edge)->color;
// If the color changes from prevEdge to edge, this is a corner.
if (!(commonColor&(commonColor-1))) {
// Find the four texels that envelop the corner and mark them as protected.
Point2 p = projection.project((*edge)->point(0));
if (shape.inverseYAxis)
p.y = stencil.height-p.y;
int l = (int) floor(p.x-.5);
int b = (int) floor(p.y-.5);
int r = l+1;
int t = b+1;
// Check that the positions are within bounds.
if (l < stencil.width && b < stencil.height && r >= 0 && t >= 0) {
if (l >= 0 && b >= 0)
*stencil(l, b) |= (byte) PROTECTED;
if (r < stencil.width && b >= 0)
*stencil(r, b) |= (byte) PROTECTED;
if (l >= 0 && t < stencil.height)
*stencil(l, t) |= (byte) PROTECTED;
if (r < stencil.width && t < stencil.height)
*stencil(r, t) |= (byte) PROTECTED;
}
}
prevEdge = *edge;
}
}
}
/// Determines if the channel contributes to an edge between the two texels a, b.
static bool edgeBetweenTexelsChannel(const float *a, const float *b, int channel) {
// Find interpolation ratio t (0 < t < 1) where an edge is expected (mix(a[channel], b[channel], t) == 0.5).
double t = (a[channel]-.5)/(a[channel]-b[channel]);
if (t > 0 && t < 1) {
// Interpolate channel values at t.
float c[3] = {
mix(a[0], b[0], t),
mix(a[1], b[1], t),
mix(a[2], b[2], t)
};
// This is only an edge if the zero-distance channel is the median.
return median(c[0], c[1], c[2]) == c[channel];
}
return false;
}
/// Returns a bit mask of which channels contribute to an edge between the two texels a, b.
static int edgeBetweenTexels(const float *a, const float *b) {
return (
RED*edgeBetweenTexelsChannel(a, b, 0)+
GREEN*edgeBetweenTexelsChannel(a, b, 1)+
BLUE*edgeBetweenTexelsChannel(a, b, 2)
);
}
/// Marks texel as protected if one of its non-median channels is present in the channel mask.
static void protectExtremeChannels(byte *stencil, const float *msd, float m, int mask) {
if (
(mask&RED && msd[0] != m) ||
(mask&GREEN && msd[1] != m) ||
(mask&BLUE && msd[2] != m)
)
*stencil |= (byte) MSDFErrorCorrection::PROTECTED;
}
template <int N>
void MSDFErrorCorrection::protectEdges(const BitmapConstRef<float, N> &sdf) {
float radius;
// Horizontal texel pairs
radius = float(PROTECTION_RADIUS_TOLERANCE*projection.unprojectVector(Vector2(invRange, 0)).length());
for (int y = 0; y < sdf.height; ++y) {
const float *left = sdf(0, y);
const float *right = sdf(1, y);
for (int x = 0; x < sdf.width-1; ++x) {
float lm = median(left[0], left[1], left[2]);
float rm = median(right[0], right[1], right[2]);
if (fabsf(lm-.5f)+fabsf(rm-.5f) < radius) {
int mask = edgeBetweenTexels(left, right);
protectExtremeChannels(stencil(x, y), left, lm, mask);
protectExtremeChannels(stencil(x+1, y), right, rm, mask);
}
left += N, right += N;
}
}
// Vertical texel pairs
radius = float(PROTECTION_RADIUS_TOLERANCE*projection.unprojectVector(Vector2(0, invRange)).length());
for (int y = 0; y < sdf.height-1; ++y) {
const float *bottom = sdf(0, y);
const float *top = sdf(0, y+1);
for (int x = 0; x < sdf.width; ++x) {
float bm = median(bottom[0], bottom[1], bottom[2]);
float tm = median(top[0], top[1], top[2]);
if (fabsf(bm-.5f)+fabsf(tm-.5f) < radius) {
int mask = edgeBetweenTexels(bottom, top);
protectExtremeChannels(stencil(x, y), bottom, bm, mask);
protectExtremeChannels(stencil(x, y+1), top, tm, mask);
}
bottom += N, top += N;
}
}
// Diagonal texel pairs
radius = float(PROTECTION_RADIUS_TOLERANCE*projection.unprojectVector(Vector2(invRange)).length());
for (int y = 0; y < sdf.height-1; ++y) {
const float *lb = sdf(0, y);
const float *rb = sdf(1, y);
const float *lt = sdf(0, y+1);
const float *rt = sdf(1, y+1);
for (int x = 0; x < sdf.width-1; ++x) {
float mlb = median(lb[0], lb[1], lb[2]);
float mrb = median(rb[0], rb[1], rb[2]);
float mlt = median(lt[0], lt[1], lt[2]);
float mrt = median(rt[0], rt[1], rt[2]);
if (fabsf(mlb-.5f)+fabsf(mrt-.5f) < radius) {
int mask = edgeBetweenTexels(lb, rt);
protectExtremeChannels(stencil(x, y), lb, mlb, mask);
protectExtremeChannels(stencil(x+1, y+1), rt, mrt, mask);
}
if (fabsf(mrb-.5f)+fabsf(mlt-.5f) < radius) {
int mask = edgeBetweenTexels(rb, lt);
protectExtremeChannels(stencil(x+1, y), rb, mrb, mask);
protectExtremeChannels(stencil(x, y+1), lt, mlt, mask);
}
lb += N, rb += N, lt += N, rt += N;
}
}
}
void MSDFErrorCorrection::protectAll() {
byte *end = stencil.pixels+stencil.width*stencil.height;
for (byte *mask = stencil.pixels; mask < end; ++mask)
*mask |= (byte) PROTECTED;
}
/// Returns the median of the linear interpolation of texels a, b at t.
static float interpolatedMedian(const float *a, const float *b, double t) {
return median(
mix(a[0], b[0], t),
mix(a[1], b[1], t),
mix(a[2], b[2], t)
);
}
/// Returns the median of the bilinear interpolation with the given constant, linear, and quadratic terms at t.
static float interpolatedMedian(const float *a, const float *l, const float *q, double t) {
return float(median(
t*(t*q[0]+l[0])+a[0],
t*(t*q[1]+l[1])+a[1],
t*(t*q[2]+l[2])+a[2]
));
}
/// Determines if the interpolated median xm is an artifact.
static bool isArtifact(bool isProtected, double axSpan, double bxSpan, float am, float bm, float xm) {
return (
// For protected texels, only report an artifact if it would cause fill inversion (change between positive and negative distance).
(!isProtected || (am > .5f && bm > .5f && xm <= .5f) || (am < .5f && bm < .5f && xm >= .5f)) &&
// This is an artifact if the interpolated median is outside the range of possible values based on its distance from a, b.
!(xm >= am-axSpan && xm <= am+axSpan && xm >= bm-bxSpan && xm <= bm+bxSpan)
);
}
/// Checks if a linear interpolation artifact will occur at a point where two specific color channels are equal - such points have extreme median values.
template <class ArtifactClassifier>
static bool hasLinearArtifactInner(const ArtifactClassifier &artifactClassifier, float am, float bm, const float *a, const float *b, float dA, float dB) {
// Find interpolation ratio t (0 < t < 1) where two color channels are equal (mix(dA, dB, t) == 0).
double t = (double) dA/(dA-dB);
if (t > ARTIFACT_T_EPSILON && t < 1-ARTIFACT_T_EPSILON) {
// Interpolate median at t and let the classifier decide if its value indicates an artifact.
float xm = interpolatedMedian(a, b, t);
return artifactClassifier.evaluate(t, xm, artifactClassifier.rangeTest(0, 1, t, am, bm, xm));
}
return false;
}
/// Checks if a bilinear interpolation artifact will occur at a point where two specific color channels are equal - such points have extreme median values.
template <class ArtifactClassifier>
static bool hasDiagonalArtifactInner(const ArtifactClassifier &artifactClassifier, float am, float dm, const float *a, const float *l, const float *q, float dA, float dBC, float dD, double tEx0, double tEx1) {
// Find interpolation ratios t (0 < t[i] < 1) where two color channels are equal.
double t[2];
int solutions = solveQuadratic(t, dD-dBC+dA, dBC-dA-dA, dA);
for (int i = 0; i < solutions; ++i) {
// Solutions t[i] == 0 and t[i] == 1 are singularities and occur very often because two channels are usually equal at texels.
if (t[i] > ARTIFACT_T_EPSILON && t[i] < 1-ARTIFACT_T_EPSILON) {
// Interpolate median xm at t.
float xm = interpolatedMedian(a, l, q, t[i]);
// Determine if xm deviates too much from medians of a, d.
int rangeFlags = artifactClassifier.rangeTest(0, 1, t[i], am, dm, xm);
// Additionally, check xm against the interpolated medians at the local extremes tEx0, tEx1.
double tEnd[2];
float em[2];
// tEx0
if (tEx0 > 0 && tEx0 < 1) {
tEnd[0] = 0, tEnd[1] = 1;
em[0] = am, em[1] = dm;
tEnd[tEx0 > t[i]] = tEx0;
em[tEx0 > t[i]] = interpolatedMedian(a, l, q, tEx0);
rangeFlags |= artifactClassifier.rangeTest(tEnd[0], tEnd[1], t[i], am, dm, xm);
}
// tEx1
if (tEx1 > 0 && tEx1 < 1) {
tEnd[0] = 0, tEnd[1] = 1;
em[0] = am, em[1] = dm;
tEnd[tEx1 > t[i]] = tEx1;
em[tEx1 > t[i]] = interpolatedMedian(a, l, q, tEx1);
rangeFlags |= artifactClassifier.rangeTest(tEnd[0], tEnd[1], t[i], am, dm, xm);
}
if (artifactClassifier.evaluate(t[i], xm, rangeFlags))
return true;
}
}
return false;
}
/// Checks if a linear interpolation artifact will occur inbetween two horizontally or vertically adjacent texels a, b.
template <class ArtifactClassifier>
static bool hasLinearArtifact(const ArtifactClassifier &artifactClassifier, float am, const float *a, const float *b) {
float bm = median(b[0], b[1], b[2]);
return (
// Out of the pair, only report artifacts for the texel further from the edge to minimize side effects.
fabsf(am-.5f) >= fabsf(bm-.5f) && (
// Check points where each pair of color channels meets.
hasLinearArtifactInner(artifactClassifier, am, bm, a, b, a[1]-a[0], b[1]-b[0]) ||
hasLinearArtifactInner(artifactClassifier, am, bm, a, b, a[2]-a[1], b[2]-b[1]) ||
hasLinearArtifactInner(artifactClassifier, am, bm, a, b, a[0]-a[2], b[0]-b[2])
)
);
}
/// Checks if a bilinear interpolation artifact will occur inbetween two diagonally adjacent texels a, d (with b, c forming the other diagonal).
template <class ArtifactClassifier>
static bool hasDiagonalArtifact(const ArtifactClassifier &artifactClassifier, float am, const float *a, const float *b, const float *c, const float *d) {
float dm = median(d[0], d[1], d[2]);
// Out of the pair, only report artifacts for the texel further from the edge to minimize side effects.
if (fabsf(am-.5f) >= fabsf(dm-.5f)) {
float abc[3] = {
a[0]-b[0]-c[0],
a[1]-b[1]-c[1],
a[2]-b[2]-c[2]
};
// Compute the linear terms for bilinear interpolation.
float l[3] = {
-a[0]-abc[0],
-a[1]-abc[1],
-a[2]-abc[2]
};
// Compute the quadratic terms for bilinear interpolation.
float q[3] = {
d[0]+abc[0],
d[1]+abc[1],
d[2]+abc[2]
};
// Compute interpolation ratios tEx (0 < tEx[i] < 1) for the local extremes of each color channel (the derivative 2*q[i]*tEx[i]+l[i] == 0).
double tEx[3] = {
-.5*l[0]/q[0],
-.5*l[1]/q[1],
-.5*l[2]/q[2]
};
// Check points where each pair of color channels meets.
return (
hasDiagonalArtifactInner(artifactClassifier, am, dm, a, l, q, a[1]-a[0], b[1]-b[0]+c[1]-c[0], d[1]-d[0], tEx[0], tEx[1]) ||
hasDiagonalArtifactInner(artifactClassifier, am, dm, a, l, q, a[2]-a[1], b[2]-b[1]+c[2]-c[1], d[2]-d[1], tEx[1], tEx[2]) ||
hasDiagonalArtifactInner(artifactClassifier, am, dm, a, l, q, a[0]-a[2], b[0]-b[2]+c[0]-c[2], d[0]-d[2], tEx[2], tEx[0])
);
}
return false;
}
template <int N>
void MSDFErrorCorrection::findErrors(const BitmapConstRef<float, N> &sdf) {
// Compute the expected deltas between values of horizontally, vertically, and diagonally adjacent texels.
double hSpan = minDeviationRatio*projection.unprojectVector(Vector2(invRange, 0)).length();
double vSpan = minDeviationRatio*projection.unprojectVector(Vector2(0, invRange)).length();
double dSpan = minDeviationRatio*projection.unprojectVector(Vector2(invRange)).length();
// Inspect all texels.
for (int y = 0; y < sdf.height; ++y) {
for (int x = 0; x < sdf.width; ++x) {
const float *c = sdf(x, y);
float cm = median(c[0], c[1], c[2]);
bool protectedFlag = (*stencil(x, y)&PROTECTED) != 0;
const float *l = NULL, *b = NULL, *r = NULL, *t = NULL;
// Mark current texel c with the error flag if an artifact occurs when it's interpolated with any of its 8 neighbors.
*stencil(x, y) |= (byte) (ERROR*(
(x > 0 && ((l = sdf(x-1, y)), hasLinearArtifact(BaseArtifactClassifier(hSpan, protectedFlag), cm, c, l))) ||
(y > 0 && ((b = sdf(x, y-1)), hasLinearArtifact(BaseArtifactClassifier(vSpan, protectedFlag), cm, c, b))) ||
(x < sdf.width-1 && ((r = sdf(x+1, y)), hasLinearArtifact(BaseArtifactClassifier(hSpan, protectedFlag), cm, c, r))) ||
(y < sdf.height-1 && ((t = sdf(x, y+1)), hasLinearArtifact(BaseArtifactClassifier(vSpan, protectedFlag), cm, c, t))) ||
(x > 0 && y > 0 && hasDiagonalArtifact(BaseArtifactClassifier(dSpan, protectedFlag), cm, c, l, b, sdf(x-1, y-1))) ||
(x < sdf.width-1 && y > 0 && hasDiagonalArtifact(BaseArtifactClassifier(dSpan, protectedFlag), cm, c, r, b, sdf(x+1, y-1))) ||
(x > 0 && y < sdf.height-1 && hasDiagonalArtifact(BaseArtifactClassifier(dSpan, protectedFlag), cm, c, l, t, sdf(x-1, y+1))) ||
(x < sdf.width-1 && y < sdf.height-1 && hasDiagonalArtifact(BaseArtifactClassifier(dSpan, protectedFlag), cm, c, r, t, sdf(x+1, y+1)))
));
}
}
}
template <template <typename> class ContourCombiner, int N>
void MSDFErrorCorrection::findErrors(const BitmapConstRef<float, N> &sdf, const Shape &shape) {
// Compute the expected deltas between values of horizontally, vertically, and diagonally adjacent texels.
double hSpan = minDeviationRatio*projection.unprojectVector(Vector2(invRange, 0)).length();
double vSpan = minDeviationRatio*projection.unprojectVector(Vector2(0, invRange)).length();
double dSpan = minDeviationRatio*projection.unprojectVector(Vector2(invRange)).length();
#ifdef MSDFGEN_USE_OPENMP
#pragma omp parallel
#endif
{
ShapeDistanceChecker<ContourCombiner, N> shapeDistanceChecker(sdf, shape, projection, invRange, minImproveRatio);
bool rightToLeft = false;
// Inspect all texels.
#ifdef MSDFGEN_USE_OPENMP
#pragma omp for
#endif
for (int y = 0; y < sdf.height; ++y) {
int row = shape.inverseYAxis ? sdf.height-y-1 : y;
for (int col = 0; col < sdf.width; ++col) {
int x = rightToLeft ? sdf.width-col-1 : col;
if ((*stencil(x, row)&ERROR))
continue;
const float *c = sdf(x, row);
shapeDistanceChecker.shapeCoord = projection.unproject(Point2(x+.5, y+.5));
shapeDistanceChecker.sdfCoord = Point2(x+.5, row+.5);
shapeDistanceChecker.msd = c;
shapeDistanceChecker.protectedFlag = (*stencil(x, row)&PROTECTED) != 0;
float cm = median(c[0], c[1], c[2]);
const float *l = NULL, *b = NULL, *r = NULL, *t = NULL;
// Mark current texel c with the error flag if an artifact occurs when it's interpolated with any of its 8 neighbors.
*stencil(x, row) |= (byte) (ERROR*(
(x > 0 && ((l = sdf(x-1, row)), hasLinearArtifact(shapeDistanceChecker.classifier(Vector2(-1, 0), hSpan), cm, c, l))) ||
(row > 0 && ((b = sdf(x, row-1)), hasLinearArtifact(shapeDistanceChecker.classifier(Vector2(0, -1), vSpan), cm, c, b))) ||
(x < sdf.width-1 && ((r = sdf(x+1, row)), hasLinearArtifact(shapeDistanceChecker.classifier(Vector2(+1, 0), hSpan), cm, c, r))) ||
(row < sdf.height-1 && ((t = sdf(x, row+1)), hasLinearArtifact(shapeDistanceChecker.classifier(Vector2(0, +1), vSpan), cm, c, t))) ||
(x > 0 && row > 0 && hasDiagonalArtifact(shapeDistanceChecker.classifier(Vector2(-1, -1), dSpan), cm, c, l, b, sdf(x-1, row-1))) ||
(x < sdf.width-1 && row > 0 && hasDiagonalArtifact(shapeDistanceChecker.classifier(Vector2(+1, -1), dSpan), cm, c, r, b, sdf(x+1, row-1))) ||
(x > 0 && row < sdf.height-1 && hasDiagonalArtifact(shapeDistanceChecker.classifier(Vector2(-1, +1), dSpan), cm, c, l, t, sdf(x-1, row+1))) ||
(x < sdf.width-1 && row < sdf.height-1 && hasDiagonalArtifact(shapeDistanceChecker.classifier(Vector2(+1, +1), dSpan), cm, c, r, t, sdf(x+1, row+1)))
));
}
}
}
}
template <int N>
void MSDFErrorCorrection::apply(const BitmapRef<float, N> &sdf) const {
int texelCount = sdf.width*sdf.height;
const byte *mask = stencil.pixels;
float *texel = sdf.pixels;
for (int i = 0; i < texelCount; ++i) {
if (*mask&ERROR) {
// Set all color channels to the median.
float m = median(texel[0], texel[1], texel[2]);
texel[0] = m, texel[1] = m, texel[2] = m;
}
++mask;
texel += N;
}
}
BitmapConstRef<byte, 1> MSDFErrorCorrection::getStencil() const {
return stencil;
}
template void MSDFErrorCorrection::protectEdges(const BitmapConstRef<float, 3> &sdf);
template void MSDFErrorCorrection::protectEdges(const BitmapConstRef<float, 4> &sdf);
template void MSDFErrorCorrection::findErrors(const BitmapConstRef<float, 3> &sdf);
template void MSDFErrorCorrection::findErrors(const BitmapConstRef<float, 4> &sdf);
template void MSDFErrorCorrection::findErrors<SimpleContourCombiner>(const BitmapConstRef<float, 3> &sdf, const Shape &shape);
template void MSDFErrorCorrection::findErrors<SimpleContourCombiner>(const BitmapConstRef<float, 4> &sdf, const Shape &shape);
template void MSDFErrorCorrection::findErrors<OverlappingContourCombiner>(const BitmapConstRef<float, 3> &sdf, const Shape &shape);
template void MSDFErrorCorrection::findErrors<OverlappingContourCombiner>(const BitmapConstRef<float, 4> &sdf, const Shape &shape);
template void MSDFErrorCorrection::apply(const BitmapRef<float, 3> &sdf) const;
template void MSDFErrorCorrection::apply(const BitmapRef<float, 4> &sdf) const;
}

View File

@ -0,0 +1,56 @@
#pragma once
#include "Projection.h"
#include "Shape.h"
#include "BitmapRef.hpp"
namespace msdfgen {
/// Performs error correction on a computed MSDF to eliminate interpolation artifacts. This is a low-level class, you may want to use the API in msdf-error-correction.h instead.
class MSDFErrorCorrection {
public:
/// Stencil flags.
enum Flags {
/// Texel marked as potentially causing interpolation errors.
ERROR = 1,
/// Texel marked as protected. Protected texels are only given the error flag if they cause inversion artifacts.
PROTECTED = 2
};
MSDFErrorCorrection();
explicit MSDFErrorCorrection(const BitmapRef<byte, 1> &stencil, const Projection &projection, double range);
/// Sets the minimum ratio between the actual and maximum expected distance delta to be considered an error.
void setMinDeviationRatio(double minDeviationRatio);
/// Sets the minimum ratio between the pre-correction distance error and the post-correction distance error.
void setMinImproveRatio(double minImproveRatio);
/// Flags all texels that are interpolated at corners as protected.
void protectCorners(const Shape &shape);
/// Flags all texels that contribute to edges as protected.
template <int N>
void protectEdges(const BitmapConstRef<float, N> &sdf);
/// Flags all texels as protected.
void protectAll();
/// Flags texels that are expected to cause interpolation artifacts based on analysis of the SDF only.
template <int N>
void findErrors(const BitmapConstRef<float, N> &sdf);
/// Flags texels that are expected to cause interpolation artifacts based on analysis of the SDF and comparison with the exact shape distance.
template <template <typename> class ContourCombiner, int N>
void findErrors(const BitmapConstRef<float, N> &sdf, const Shape &shape);
/// Modifies the MSDF so that all texels with the error flag are converted to single-channel.
template <int N>
void apply(const BitmapRef<float, N> &sdf) const;
/// Returns the stencil in its current state (see Flags).
BitmapConstRef<byte, 1> getStencil() const;
private:
BitmapRef<byte, 1> stencil;
Projection projection;
double invRange;
double minDeviationRatio;
double minImproveRatio;
};
}

42
thirdparty/msdfgen/core/Projection.cpp vendored Normal file
View File

@ -0,0 +1,42 @@
#include "Projection.h"
namespace msdfgen {
Projection::Projection() : scale(1), translate(0) { }
Projection::Projection(const Vector2 &scale, const Vector2 &translate) : scale(scale), translate(translate) { }
Point2 Projection::project(const Point2 &coord) const {
return scale*(coord+translate);
}
Point2 Projection::unproject(const Point2 &coord) const {
return coord/scale-translate;
}
Vector2 Projection::projectVector(const Vector2 &vector) const {
return scale*vector;
}
Vector2 Projection::unprojectVector(const Vector2 &vector) const {
return vector/scale;
}
double Projection::projectX(double x) const {
return scale.x*(x+translate.x);
}
double Projection::projectY(double y) const {
return scale.y*(y+translate.y);
}
double Projection::unprojectX(double x) const {
return x/scale.x-translate.x;
}
double Projection::unprojectY(double y) const {
return y/scale.y-translate.y;
}
}

37
thirdparty/msdfgen/core/Projection.h vendored Normal file
View File

@ -0,0 +1,37 @@
#pragma once
#include "Vector2.h"
namespace msdfgen {
/// A transformation from shape coordinates to pixel coordinates.
class Projection {
public:
Projection();
Projection(const Vector2 &scale, const Vector2 &translate);
/// Converts the shape coordinate to pixel coordinate.
Point2 project(const Point2 &coord) const;
/// Converts the pixel coordinate to shape coordinate.
Point2 unproject(const Point2 &coord) const;
/// Converts the vector to pixel coordinate space.
Vector2 projectVector(const Vector2 &vector) const;
/// Converts the vector from pixel coordinate space.
Vector2 unprojectVector(const Vector2 &vector) const;
/// Converts the X-coordinate from shape to pixel coordinate space.
double projectX(double x) const;
/// Converts the Y-coordinate from shape to pixel coordinate space.
double projectY(double y) const;
/// Converts the X-coordinate from pixel to shape coordinate space.
double unprojectX(double x) const;
/// Converts the Y-coordinate from pixel to shape coordinate space.
double unprojectY(double y) const;
private:
Vector2 scale;
Vector2 translate;
};
}

125
thirdparty/msdfgen/core/Scanline.cpp vendored Normal file
View File

@ -0,0 +1,125 @@
#include "Scanline.h"
#include <algorithm>
#include "arithmetics.hpp"
namespace msdfgen {
static int compareIntersections(const void *a, const void *b) {
return sign(reinterpret_cast<const Scanline::Intersection *>(a)->x-reinterpret_cast<const Scanline::Intersection *>(b)->x);
}
bool interpretFillRule(int intersections, FillRule fillRule) {
switch (fillRule) {
case FILL_NONZERO:
return intersections != 0;
case FILL_ODD:
return intersections&1;
case FILL_POSITIVE:
return intersections > 0;
case FILL_NEGATIVE:
return intersections < 0;
}
return false;
}
double Scanline::overlap(const Scanline &a, const Scanline &b, double xFrom, double xTo, FillRule fillRule) {
double total = 0;
bool aInside = false, bInside = false;
int ai = 0, bi = 0;
double ax = !a.intersections.empty() ? a.intersections[ai].x : xTo;
double bx = !b.intersections.empty() ? b.intersections[bi].x : xTo;
while (ax < xFrom || bx < xFrom) {
double xNext = min(ax, bx);
if (ax == xNext && ai < (int) a.intersections.size()) {
aInside = interpretFillRule(a.intersections[ai].direction, fillRule);
ax = ++ai < (int) a.intersections.size() ? a.intersections[ai].x : xTo;
}
if (bx == xNext && bi < (int) b.intersections.size()) {
bInside = interpretFillRule(b.intersections[bi].direction, fillRule);
bx = ++bi < (int) b.intersections.size() ? b.intersections[bi].x : xTo;
}
}
double x = xFrom;
while (ax < xTo || bx < xTo) {
double xNext = min(ax, bx);
if (aInside == bInside)
total += xNext-x;
if (ax == xNext && ai < (int) a.intersections.size()) {
aInside = interpretFillRule(a.intersections[ai].direction, fillRule);
ax = ++ai < (int) a.intersections.size() ? a.intersections[ai].x : xTo;
}
if (bx == xNext && bi < (int) b.intersections.size()) {
bInside = interpretFillRule(b.intersections[bi].direction, fillRule);
bx = ++bi < (int) b.intersections.size() ? b.intersections[bi].x : xTo;
}
x = xNext;
}
if (aInside == bInside)
total += xTo-x;
return total;
}
Scanline::Scanline() : lastIndex(0) { }
void Scanline::preprocess() {
lastIndex = 0;
if (!intersections.empty()) {
qsort(&intersections[0], intersections.size(), sizeof(Intersection), compareIntersections);
int totalDirection = 0;
for (std::vector<Intersection>::iterator intersection = intersections.begin(); intersection != intersections.end(); ++intersection) {
totalDirection += intersection->direction;
intersection->direction = totalDirection;
}
}
}
void Scanline::setIntersections(const std::vector<Intersection> &intersections) {
this->intersections = intersections;
preprocess();
}
#ifdef MSDFGEN_USE_CPP11
void Scanline::setIntersections(std::vector<Intersection> &&intersections) {
this->intersections = (std::vector<Intersection> &&) intersections;
preprocess();
}
#endif
int Scanline::moveTo(double x) const {
if (intersections.empty())
return -1;
int index = lastIndex;
if (x < intersections[index].x) {
do {
if (index == 0) {
lastIndex = 0;
return -1;
}
--index;
} while (x < intersections[index].x);
} else {
while (index < (int) intersections.size()-1 && x >= intersections[index+1].x)
++index;
}
lastIndex = index;
return index;
}
int Scanline::countIntersections(double x) const {
return moveTo(x)+1;
}
int Scanline::sumIntersections(double x) const {
int index = moveTo(x);
if (index >= 0)
return intersections[index].direction;
return 0;
}
bool Scanline::filled(double x, FillRule fillRule) const {
return interpretFillRule(sumIntersections(x), fillRule);
}
}

55
thirdparty/msdfgen/core/Scanline.h vendored Normal file
View File

@ -0,0 +1,55 @@
#pragma once
#include <vector>
namespace msdfgen {
/// Fill rule dictates how intersection total is interpreted during rasterization.
enum FillRule {
FILL_NONZERO,
FILL_ODD, // "even-odd"
FILL_POSITIVE,
FILL_NEGATIVE
};
/// Resolves the number of intersection into a binary fill value based on fill rule.
bool interpretFillRule(int intersections, FillRule fillRule);
/// Represents a horizontal scanline intersecting a shape.
class Scanline {
public:
/// An intersection with the scanline.
struct Intersection {
/// X coordinate.
double x;
/// Normalized Y direction of the oriented edge at the point of intersection.
int direction;
};
static double overlap(const Scanline &a, const Scanline &b, double xFrom, double xTo, FillRule fillRule);
Scanline();
/// Populates the intersection list.
void setIntersections(const std::vector<Intersection> &intersections);
#ifdef MSDFGEN_USE_CPP11
void setIntersections(std::vector<Intersection> &&intersections);
#endif
/// Returns the number of intersections left of x.
int countIntersections(double x) const;
/// Returns the total sign of intersections left of x.
int sumIntersections(double x) const;
/// Decides whether the scanline is filled at x based on fill rule.
bool filled(double x, FillRule fillRule) const;
private:
std::vector<Intersection> intersections;
mutable int lastIndex;
void preprocess();
int moveTo(double x) const;
};
}

183
thirdparty/msdfgen/core/Shape.cpp vendored Normal file
View File

@ -0,0 +1,183 @@
#include "Shape.h"
#include <algorithm>
#include "arithmetics.hpp"
namespace msdfgen {
Shape::Shape() : inverseYAxis(false) { }
void Shape::addContour(const Contour &contour) {
contours.push_back(contour);
}
#ifdef MSDFGEN_USE_CPP11
void Shape::addContour(Contour &&contour) {
contours.push_back((Contour &&) contour);
}
#endif
Contour & Shape::addContour() {
contours.resize(contours.size()+1);
return contours.back();
}
bool Shape::validate() const {
for (std::vector<Contour>::const_iterator contour = contours.begin(); contour != contours.end(); ++contour) {
if (!contour->edges.empty()) {
Point2 corner = contour->edges.back()->point(1);
for (std::vector<EdgeHolder>::const_iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge) {
if (!*edge)
return false;
if ((*edge)->point(0) != corner)
return false;
corner = (*edge)->point(1);
}
}
}
return true;
}
static void deconvergeEdge(EdgeHolder &edgeHolder, int param) {
{
const QuadraticSegment *quadraticSegment = dynamic_cast<const QuadraticSegment *>(&*edgeHolder);
if (quadraticSegment)
edgeHolder = quadraticSegment->convertToCubic();
}
{
CubicSegment *cubicSegment = dynamic_cast<CubicSegment *>(&*edgeHolder);
if (cubicSegment)
cubicSegment->deconverge(param, MSDFGEN_DECONVERGENCE_FACTOR);
}
}
void Shape::normalize() {
for (std::vector<Contour>::iterator contour = contours.begin(); contour != contours.end(); ++contour) {
if (contour->edges.size() == 1) {
EdgeSegment *parts[3] = { };
contour->edges[0]->splitInThirds(parts[0], parts[1], parts[2]);
contour->edges.clear();
contour->edges.push_back(EdgeHolder(parts[0]));
contour->edges.push_back(EdgeHolder(parts[1]));
contour->edges.push_back(EdgeHolder(parts[2]));
} else {
EdgeHolder *prevEdge = &contour->edges.back();
for (std::vector<EdgeHolder>::iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge) {
Vector2 prevDir = (*prevEdge)->direction(1).normalize();
Vector2 curDir = (*edge)->direction(0).normalize();
if (dotProduct(prevDir, curDir) < MSDFGEN_CORNER_DOT_EPSILON-1) {
deconvergeEdge(*prevEdge, 1);
deconvergeEdge(*edge, 0);
}
prevEdge = &*edge;
}
}
}
}
void Shape::bound(double &l, double &b, double &r, double &t) const {
for (std::vector<Contour>::const_iterator contour = contours.begin(); contour != contours.end(); ++contour)
contour->bound(l, b, r, t);
}
void Shape::boundMiters(double &l, double &b, double &r, double &t, double border, double miterLimit, int polarity) const {
for (std::vector<Contour>::const_iterator contour = contours.begin(); contour != contours.end(); ++contour)
contour->boundMiters(l, b, r, t, border, miterLimit, polarity);
}
Shape::Bounds Shape::getBounds(double border, double miterLimit, int polarity) const {
static const double LARGE_VALUE = 1e240;
Shape::Bounds bounds = { +LARGE_VALUE, +LARGE_VALUE, -LARGE_VALUE, -LARGE_VALUE };
bound(bounds.l, bounds.b, bounds.r, bounds.t);
if (border > 0) {
bounds.l -= border, bounds.b -= border;
bounds.r += border, bounds.t += border;
if (miterLimit > 0)
boundMiters(bounds.l, bounds.b, bounds.r, bounds.t, border, miterLimit, polarity);
}
return bounds;
}
void Shape::scanline(Scanline &line, double y) const {
std::vector<Scanline::Intersection> intersections;
double x[3];
int dy[3];
for (std::vector<Contour>::const_iterator contour = contours.begin(); contour != contours.end(); ++contour) {
for (std::vector<EdgeHolder>::const_iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge) {
int n = (*edge)->scanlineIntersections(x, dy, y);
for (int i = 0; i < n; ++i) {
Scanline::Intersection intersection = { x[i], dy[i] };
intersections.push_back(intersection);
}
}
}
#ifdef MSDFGEN_USE_CPP11
line.setIntersections((std::vector<Scanline::Intersection> &&) intersections);
#else
line.setIntersections(intersections);
#endif
}
int Shape::edgeCount() const {
int total = 0;
for (std::vector<Contour>::const_iterator contour = contours.begin(); contour != contours.end(); ++contour)
total += (int) contour->edges.size();
return total;
}
void Shape::orientContours() {
struct Intersection {
double x;
int direction;
int contourIndex;
static int compare(const void *a, const void *b) {
return sign(reinterpret_cast<const Intersection *>(a)->x-reinterpret_cast<const Intersection *>(b)->x);
}
};
const double ratio = .5*(sqrt(5)-1); // an irrational number to minimize chance of intersecting a corner or other point of interest
std::vector<int> orientations(contours.size());
std::vector<Intersection> intersections;
for (int i = 0; i < (int) contours.size(); ++i) {
if (!orientations[i] && !contours[i].edges.empty()) {
// Find an Y that crosses the contour
double y0 = contours[i].edges.front()->point(0).y;
double y1 = y0;
for (std::vector<EdgeHolder>::const_iterator edge = contours[i].edges.begin(); edge != contours[i].edges.end() && y0 == y1; ++edge)
y1 = (*edge)->point(1).y;
for (std::vector<EdgeHolder>::const_iterator edge = contours[i].edges.begin(); edge != contours[i].edges.end() && y0 == y1; ++edge)
y1 = (*edge)->point(ratio).y; // in case all endpoints are in a horizontal line
double y = mix(y0, y1, ratio);
// Scanline through whole shape at Y
double x[3];
int dy[3];
for (int j = 0; j < (int) contours.size(); ++j) {
for (std::vector<EdgeHolder>::const_iterator edge = contours[j].edges.begin(); edge != contours[j].edges.end(); ++edge) {
int n = (*edge)->scanlineIntersections(x, dy, y);
for (int k = 0; k < n; ++k) {
Intersection intersection = { x[k], dy[k], j };
intersections.push_back(intersection);
}
}
}
qsort(&intersections[0], intersections.size(), sizeof(Intersection), &Intersection::compare);
// Disqualify multiple intersections
for (int j = 1; j < (int) intersections.size(); ++j)
if (intersections[j].x == intersections[j-1].x)
intersections[j].direction = intersections[j-1].direction = 0;
// Inspect scanline and deduce orientations of intersected contours
for (int j = 0; j < (int) intersections.size(); ++j)
if (intersections[j].direction)
orientations[intersections[j].contourIndex] += 2*((j&1)^(intersections[j].direction > 0))-1;
intersections.clear();
}
}
// Reverse contours that have the opposite orientation
for (int i = 0; i < (int) contours.size(); ++i)
if (orientations[i] < 0)
contours[i].reverse();
}
}

55
thirdparty/msdfgen/core/Shape.h vendored Normal file
View File

@ -0,0 +1,55 @@
#pragma once
#include <vector>
#include "Contour.h"
#include "Scanline.h"
namespace msdfgen {
// Threshold of the dot product of adjacent edge directions to be considered convergent.
#define MSDFGEN_CORNER_DOT_EPSILON .000001
// The proportional amount by which a curve's control point will be adjusted to eliminate convergent corners.
#define MSDFGEN_DECONVERGENCE_FACTOR .000001
/// Vector shape representation.
class Shape {
public:
struct Bounds {
double l, b, r, t;
};
/// The list of contours the shape consists of.
std::vector<Contour> contours;
/// Specifies whether the shape uses bottom-to-top (false) or top-to-bottom (true) Y coordinates.
bool inverseYAxis;
Shape();
/// Adds a contour.
void addContour(const Contour &contour);
#ifdef MSDFGEN_USE_CPP11
void addContour(Contour &&contour);
#endif
/// Adds a blank contour and returns its reference.
Contour & addContour();
/// Normalizes the shape geometry for distance field generation.
void normalize();
/// Performs basic checks to determine if the object represents a valid shape.
bool validate() const;
/// Adjusts the bounding box to fit the shape.
void bound(double &l, double &b, double &r, double &t) const;
/// Adjusts the bounding box to fit the shape border's mitered corners.
void boundMiters(double &l, double &b, double &r, double &t, double border, double miterLimit, int polarity) const;
/// Computes the minimum bounding box that fits the shape, optionally with a (mitered) border.
Bounds getBounds(double border = 0, double miterLimit = 0, int polarity = 0) const;
/// Outputs the scanline that intersects the shape at y.
void scanline(Scanline &line, double y) const;
/// Returns the total number of edge segments
int edgeCount() const;
/// Assumes its contours are unoriented (even-odd fill rule). Attempts to orient them to conform to the non-zero winding rule.
void orientContours();
};
}

View File

@ -0,0 +1,37 @@
#pragma once
#include <vector>
#include "Vector2.h"
#include "edge-selectors.h"
#include "contour-combiners.h"
namespace msdfgen {
/// Finds the distance between a point and a Shape. ContourCombiner dictates the distance metric and its data type.
template <class ContourCombiner>
class ShapeDistanceFinder {
public:
typedef typename ContourCombiner::DistanceType DistanceType;
// Passed shape object must persist until the distance finder is destroyed!
explicit ShapeDistanceFinder(const Shape &shape);
/// Finds the distance from origin. Not thread-safe! Is fastest when subsequent queries are close together.
DistanceType distance(const Point2 &origin);
/// Finds the distance between shape and origin. Does not allocate result cache used to optimize performance of multiple queries.
static DistanceType oneShotDistance(const Shape &shape, const Point2 &origin);
private:
const Shape &shape;
ContourCombiner contourCombiner;
std::vector<typename ContourCombiner::EdgeSelectorType::EdgeCache> shapeEdgeCache;
};
typedef ShapeDistanceFinder<SimpleContourCombiner<TrueDistanceSelector> > SimpleTrueShapeDistanceFinder;
}
#include "ShapeDistanceFinder.hpp"

View File

@ -0,0 +1,56 @@
#include "ShapeDistanceFinder.h"
namespace msdfgen {
template <class ContourCombiner>
ShapeDistanceFinder<ContourCombiner>::ShapeDistanceFinder(const Shape &shape) : shape(shape), contourCombiner(shape), shapeEdgeCache(shape.edgeCount()) { }
template <class ContourCombiner>
typename ShapeDistanceFinder<ContourCombiner>::DistanceType ShapeDistanceFinder<ContourCombiner>::distance(const Point2 &origin) {
contourCombiner.reset(origin);
typename ContourCombiner::EdgeSelectorType::EdgeCache *edgeCache = &shapeEdgeCache[0];
for (std::vector<Contour>::const_iterator contour = shape.contours.begin(); contour != shape.contours.end(); ++contour) {
if (!contour->edges.empty()) {
typename ContourCombiner::EdgeSelectorType &edgeSelector = contourCombiner.edgeSelector(int(contour-shape.contours.begin()));
const EdgeSegment *prevEdge = contour->edges.size() >= 2 ? *(contour->edges.end()-2) : *contour->edges.begin();
const EdgeSegment *curEdge = contour->edges.back();
for (std::vector<EdgeHolder>::const_iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge) {
const EdgeSegment *nextEdge = *edge;
edgeSelector.addEdge(*edgeCache++, prevEdge, curEdge, nextEdge);
prevEdge = curEdge;
curEdge = nextEdge;
}
}
}
return contourCombiner.distance();
}
template <class ContourCombiner>
typename ShapeDistanceFinder<ContourCombiner>::DistanceType ShapeDistanceFinder<ContourCombiner>::oneShotDistance(const Shape &shape, const Point2 &origin) {
ContourCombiner contourCombiner(shape);
contourCombiner.reset(origin);
for (std::vector<Contour>::const_iterator contour = shape.contours.begin(); contour != shape.contours.end(); ++contour) {
if (!contour->edges.empty()) {
typename ContourCombiner::EdgeSelectorType &edgeSelector = contourCombiner.edgeSelector(int(contour-shape.contours.begin()));
const EdgeSegment *prevEdge = contour->edges.size() >= 2 ? *(contour->edges.end()-2) : *contour->edges.begin();
const EdgeSegment *curEdge = contour->edges.back();
for (std::vector<EdgeHolder>::const_iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge) {
const EdgeSegment *nextEdge = *edge;
typename ContourCombiner::EdgeSelectorType::EdgeCache dummy;
edgeSelector.addEdge(dummy, prevEdge, curEdge, nextEdge);
prevEdge = curEdge;
curEdge = nextEdge;
}
}
}
return contourCombiner.distance();
}
}

View File

@ -0,0 +1,30 @@
#include "SignedDistance.h"
#include <cmath>
namespace msdfgen {
const SignedDistance SignedDistance::INFINITE(-1e240, 1);
SignedDistance::SignedDistance() : distance(-1e240), dot(1) { }
SignedDistance::SignedDistance(double dist, double d) : distance(dist), dot(d) { }
bool operator<(SignedDistance a, SignedDistance b) {
return fabs(a.distance) < fabs(b.distance) || (fabs(a.distance) == fabs(b.distance) && a.dot < b.dot);
}
bool operator>(SignedDistance a, SignedDistance b) {
return fabs(a.distance) > fabs(b.distance) || (fabs(a.distance) == fabs(b.distance) && a.dot > b.dot);
}
bool operator<=(SignedDistance a, SignedDistance b) {
return fabs(a.distance) < fabs(b.distance) || (fabs(a.distance) == fabs(b.distance) && a.dot <= b.dot);
}
bool operator>=(SignedDistance a, SignedDistance b) {
return fabs(a.distance) > fabs(b.distance) || (fabs(a.distance) == fabs(b.distance) && a.dot >= b.dot);
}
}

View File

@ -0,0 +1,25 @@
#pragma once
namespace msdfgen {
/// Represents a signed distance and alignment, which together can be compared to uniquely determine the closest edge segment.
class SignedDistance {
public:
static const SignedDistance INFINITE;
double distance;
double dot;
SignedDistance();
SignedDistance(double dist, double d);
friend bool operator<(SignedDistance a, SignedDistance b);
friend bool operator>(SignedDistance a, SignedDistance b);
friend bool operator<=(SignedDistance a, SignedDistance b);
friend bool operator>=(SignedDistance a, SignedDistance b);
};
}

146
thirdparty/msdfgen/core/Vector2.cpp vendored Normal file
View File

@ -0,0 +1,146 @@
#include "Vector2.h"
namespace msdfgen {
Vector2::Vector2(double val) : x(val), y(val) { }
Vector2::Vector2(double x, double y) : x(x), y(y) { }
void Vector2::reset() {
x = 0, y = 0;
}
void Vector2::set(double x, double y) {
Vector2::x = x, Vector2::y = y;
}
double Vector2::length() const {
return sqrt(x*x+y*y);
}
double Vector2::direction() const {
return atan2(y, x);
}
Vector2 Vector2::normalize(bool allowZero) const {
double len = length();
if (len == 0)
return Vector2(0, !allowZero);
return Vector2(x/len, y/len);
}
Vector2 Vector2::getOrthogonal(bool polarity) const {
return polarity ? Vector2(-y, x) : Vector2(y, -x);
}
Vector2 Vector2::getOrthonormal(bool polarity, bool allowZero) const {
double len = length();
if (len == 0)
return polarity ? Vector2(0, !allowZero) : Vector2(0, -!allowZero);
return polarity ? Vector2(-y/len, x/len) : Vector2(y/len, -x/len);
}
Vector2 Vector2::project(const Vector2 &vector, bool positive) const {
Vector2 n = normalize(true);
double t = dotProduct(vector, n);
if (positive && t <= 0)
return Vector2();
return t*n;
}
Vector2::operator const void*() const {
return x || y ? this : NULL;
}
bool Vector2::operator!() const {
return !x && !y;
}
bool Vector2::operator==(const Vector2 &other) const {
return x == other.x && y == other.y;
}
bool Vector2::operator!=(const Vector2 &other) const {
return x != other.x || y != other.y;
}
Vector2 Vector2::operator+() const {
return *this;
}
Vector2 Vector2::operator-() const {
return Vector2(-x, -y);
}
Vector2 Vector2::operator+(const Vector2 &other) const {
return Vector2(x+other.x, y+other.y);
}
Vector2 Vector2::operator-(const Vector2 &other) const {
return Vector2(x-other.x, y-other.y);
}
Vector2 Vector2::operator*(const Vector2 &other) const {
return Vector2(x*other.x, y*other.y);
}
Vector2 Vector2::operator/(const Vector2 &other) const {
return Vector2(x/other.x, y/other.y);
}
Vector2 Vector2::operator*(double value) const {
return Vector2(x*value, y*value);
}
Vector2 Vector2::operator/(double value) const {
return Vector2(x/value, y/value);
}
Vector2 & Vector2::operator+=(const Vector2 &other) {
x += other.x, y += other.y;
return *this;
}
Vector2 & Vector2::operator-=(const Vector2 &other) {
x -= other.x, y -= other.y;
return *this;
}
Vector2 & Vector2::operator*=(const Vector2 &other) {
x *= other.x, y *= other.y;
return *this;
}
Vector2 & Vector2::operator/=(const Vector2 &other) {
x /= other.x, y /= other.y;
return *this;
}
Vector2 & Vector2::operator*=(double value) {
x *= value, y *= value;
return *this;
}
Vector2 & Vector2::operator/=(double value) {
x /= value, y /= value;
return *this;
}
double dotProduct(const Vector2 &a, const Vector2 &b) {
return a.x*b.x+a.y*b.y;
}
double crossProduct(const Vector2 &a, const Vector2 &b) {
return a.x*b.y-a.y*b.x;
}
Vector2 operator*(double value, const Vector2 &vector) {
return Vector2(value*vector.x, value*vector.y);
}
Vector2 operator/(double value, const Vector2 &vector) {
return Vector2(value/vector.x, value/vector.y);
}
}

66
thirdparty/msdfgen/core/Vector2.h vendored Normal file
View File

@ -0,0 +1,66 @@
#pragma once
#include <cstdlib>
#include <cmath>
namespace msdfgen {
/**
* A 2-dimensional euclidean vector with double precision.
* Implementation based on the Vector2 template from Artery Engine.
* @author Viktor Chlumsky
*/
struct Vector2 {
double x, y;
Vector2(double val = 0);
Vector2(double x, double y);
/// Sets the vector to zero.
void reset();
/// Sets individual elements of the vector.
void set(double x, double y);
/// Returns the vector's length.
double length() const;
/// Returns the angle of the vector in radians (atan2).
double direction() const;
/// Returns the normalized vector - one that has the same direction but unit length.
Vector2 normalize(bool allowZero = false) const;
/// Returns a vector with the same length that is orthogonal to this one.
Vector2 getOrthogonal(bool polarity = true) const;
/// Returns a vector with unit length that is orthogonal to this one.
Vector2 getOrthonormal(bool polarity = true, bool allowZero = false) const;
/// Returns a vector projected along this one.
Vector2 project(const Vector2 &vector, bool positive = false) const;
operator const void *() const;
bool operator!() const;
bool operator==(const Vector2 &other) const;
bool operator!=(const Vector2 &other) const;
Vector2 operator+() const;
Vector2 operator-() const;
Vector2 operator+(const Vector2 &other) const;
Vector2 operator-(const Vector2 &other) const;
Vector2 operator*(const Vector2 &other) const;
Vector2 operator/(const Vector2 &other) const;
Vector2 operator*(double value) const;
Vector2 operator/(double value) const;
Vector2 & operator+=(const Vector2 &other);
Vector2 & operator-=(const Vector2 &other);
Vector2 & operator*=(const Vector2 &other);
Vector2 & operator/=(const Vector2 &other);
Vector2 & operator*=(double value);
Vector2 & operator/=(double value);
/// Dot product of two vectors.
friend double dotProduct(const Vector2 &a, const Vector2 &b);
/// A special version of the cross product for 2D vectors (returns scalar value).
friend double crossProduct(const Vector2 &a, const Vector2 &b);
friend Vector2 operator*(double value, const Vector2 &vector);
friend Vector2 operator/(double value, const Vector2 &vector);
};
/// A vector may also represent a point, which shall be differentiated semantically using the alias Point2.
typedef Vector2 Point2;
}

Some files were not shown because too many files have changed in this diff Show More