Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support Chinese characters #278

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
The original author doesn't maintain vis-editor any more and also reject any pull request for the editor, so I fork his repository to work on for my game.
原作者已不再维护vis-editor,本分支上的维护用于我自己的游戏。

Install to Maven Local repository (default localtion is ~/.m2/repository/)
===
./gradelw clean install

VisEditor
=========

Expand Down
2 changes: 2 additions & 0 deletions build-editor.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ buildscript {
ext.kotlinVersion = '1.1.1'

repositories {
mavenLocal()
mavenCentral()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
maven { url "https://plugins.gradle.org/m2/" }
Expand Down Expand Up @@ -53,6 +54,7 @@ allprojects {
}

repositories {
mavenLocal()
mavenCentral()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
maven { url "https://oss.sonatype.org/content/repositories/releases/" }
Expand Down
14 changes: 13 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
buildscript {
ext.kotlinVersion = '1.1.1'
ext.kotlinVersion = '1.2.20'

repositories {
mavenLocal()
mavenCentral()
maven { url "http://maven.aliyun.com/nexus/content/groups/public"}
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
maven { url "https://plugins.gradle.org/m2/" }
}
Expand All @@ -25,6 +27,7 @@ allprojects {
apply plugin: "java"
apply plugin: "nebula.provided-base"
apply plugin: "nebula.optional-base"
apply plugin: "maven"

ext {
appName = 'vis'
Expand All @@ -37,12 +40,21 @@ allprojects {
}

repositories {
mavenLocal()
mavenCentral()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
maven { url "https://oss.sonatype.org/content/repositories/releases/" }
}
}

publishing {
publications {
mavenJava(MavenPublication) {
from components.java
}
}
}

nexusStaging {
packageGroup = "com.kotcrab"
username = ossrhUsername
Expand Down
1 change: 1 addition & 0 deletions editor/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'kotlin'
apply from: '../deploy.gradle'
//pply plugin: "maven-publish"

group = 'com.kotcrab.vis'
archivesBaseName = "vis-editor"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class FontCacheModule extends ProjectModule implements WatchListener {
public static final int MIN_FONT_SIZE = 5;

public static final int DEFAULT_FONT_SIZE = 20;
public static final String DEFAULT_TEXT = "The quick brown fox jumps over the lazy dog";
public static final String DEFAULT_TEXT = "这是中文。The quick brown fox jumps over the lazy dog";

private FileAccessModule fileAccess;
private AssetsWatcherModule watcherModule;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter;
import com.badlogic.gdx.utils.ObjectMap;
import com.kotcrab.vis.ui.util.font.LazyBitmapFont;

/**
* TTF font provider that allows to get font for user provided size. TtfEditorFont can only provide fonts for one pixelsPerUnits value.
Expand All @@ -47,7 +48,7 @@ public BitmapFont get (int size) {
BitmapFont font = bitmapFonts.get(size);

if (font == null) {
font = generator.generateFont(getParameterForSize(size));
font = new LazyBitmapFont(generator, getParameterForSize(size));
font.setUseIntegerPositions(false);
font.getData().setScale(1f / pixelsPerUnit);
bitmapFonts.put(size, font);
Expand All @@ -59,6 +60,7 @@ public BitmapFont get (int size) {
private FreeTypeFontParameter getParameterForSize (int size) {
FreeTypeFontParameter parameter = new FreeTypeFontParameter();
parameter.size = size;
parameter.incremental = true;
parameter.minFilter = TextureFilter.Linear;
parameter.magFilter = TextureFilter.Linear;
return parameter;
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Wed May 10 11:00:00 CEST 2017
#Mon Nov 20 12:37:50 CST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.5-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-3.5-all.zip
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.kotcrab.vis.runtime.assets;

public class TextureAsset extends PathAsset {
@Deprecated
public TextureAsset () {
}

public TextureAsset (String relativePath) {
super(relativePath);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ public void load (Array<AssetDescriptor> dependencies, VisAssetDescriptor asset)
@Override
public void setLoaders (AssetManager assetManager) {
assetManager.setLoader(FreeTypeFontGenerator.class, new FreeTypeFontGeneratorLoader(assetManager.getFileHandleResolver()));
assetManager.setLoader(BitmapFont.class, ".ttf", new FreetypeFontLoader(assetManager.getFileHandleResolver()));
assetManager.setLoader(BitmapFont.class, ".ttf", new LazyFreetypeFontLoader(assetManager.getFileHandleResolver()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.kotcrab.vis.runtime.font;

import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.assets.loaders.FileHandleResolver;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.graphics.g2d.freetype.FreetypeFontLoader;
import com.kotcrab.vis.runtime.util.font.LazyBitmapFont;

public class LazyFreetypeFontLoader extends FreetypeFontLoader {
public LazyFreetypeFontLoader(FileHandleResolver resolver) {
super(resolver);
}

@Override
public BitmapFont loadSync(AssetManager manager, String fileName, FileHandle file, FreeTypeFontLoaderParameter parameter) {
if(parameter == null) throw new RuntimeException("FreetypeFontParameter must be set in AssetManager#load to point at a TTF file!");
FreeTypeFontGenerator generator = manager.get(parameter.fontFileName + ".gen", FreeTypeFontGenerator.class);
// BitmapFont font = generator.generateFont(parameter.fontParameters);
BitmapFont font = new LazyBitmapFont(generator, parameter.fontParameters);
return font;
}

// private FreeTypeFontGenerator.FreeTypeFontParameter getParameterForSize (int size) {
// FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
// parameter.size = size;
// parameter.incremental = true;
// parameter.minFilter = Texture.TextureFilter.Linear;
// parameter.magFilter = Texture.TextureFilter.Linear;
// return parameter;
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
import com.artemis.Aspect;
import com.artemis.ComponentMapper;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.kotcrab.vis.runtime.RuntimeConfiguration;
import com.kotcrab.vis.runtime.assets.AtlasRegionAsset;
import com.kotcrab.vis.runtime.assets.TextureAsset;
import com.kotcrab.vis.runtime.assets.TextureRegionAsset;
import com.kotcrab.vis.runtime.assets.VisAssetDescriptor;
import com.kotcrab.vis.runtime.component.AssetReference;
Expand Down Expand Up @@ -57,25 +59,36 @@ public void inserted (int entityId) {

String atlasPath;
String atlasRegion;
TextureAtlas atlas;
TextureRegion region;

if (asset instanceof TextureRegionAsset) {
TextureRegionAsset regionAsset = (TextureRegionAsset) asset;
atlasPath = sceneTextureAtlasPath;
atlasRegion = PathUtils.removeExtension(regionAsset.getPath());
atlas = manager.get(atlasPath, TextureAtlas.class);
region = atlas.findRegion(atlasRegion);

} else if (asset instanceof AtlasRegionAsset) {
AtlasRegionAsset regionAsset = (AtlasRegionAsset) asset;
atlasPath = regionAsset.getPath();
atlasRegion = regionAsset.getRegionName();
atlas = manager.get(atlasPath, TextureAtlas.class);
region = atlas.findRegion(atlasRegion);

} else if (asset instanceof TextureAsset) {
TextureAsset textureAsset = (TextureAsset) asset;
manager.load(textureAsset.getPath(), Texture.class);
manager.finishLoading();
Texture texture = manager.get(textureAsset.getPath(), Texture.class);
region = new TextureRegion(texture);
atlasRegion = textureAsset.getPath();
} else {
throw new UnsupportedAssetDescriptorException(asset);
}

TextureAtlas atlas = manager.get(atlasPath, TextureAtlas.class);
TextureRegion region = atlas.findRegion(atlasRegion);
if (region == null) throw new IllegalStateException("Can't load scene, gfx asset is missing: " + atlasRegion);

if (region == null) throw new IllegalStateException("Can't load scene, gfx asset is missing: " + atlasRegion);
VisSprite sprite = spriteCm.create(entityId);
sprite.setRegion(region);
protoCm.get(entityId).fill(sprite);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package com.kotcrab.vis.runtime.util.font;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.g2d.freetype.FreeType;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.utils.GdxRuntimeException;

import java.lang.reflect.Field;

/**
* Created by dotw on 2017/11/23.
*/
public class LazyBitmapFont extends BitmapFont {

private FreeTypeFontGenerator generator;
private FreeTypeFontGenerator.FreeTypeBitmapFontData data;
private FreeTypeFontGenerator.FreeTypeFontParameter parameter;

public LazyBitmapFont(FreeTypeFontGenerator generator, FreeTypeFontGenerator.FreeTypeFontParameter param) {
super();
if (generator == null)
throw new GdxRuntimeException("lazyBitmapFont global generator must be not null to use this constructor.");
this.generator = generator;
this.parameter = param;
this.data = new LazyBitmapFontData(generator, param.size, this);
try {
Field f = getClass().getSuperclass().getDeclaredField("data");
f.setAccessible(true);
f.set(this, data);
} catch (Exception e) {
e.printStackTrace();
}

genrateData();
}

private void genrateData() {
FreeType.Face face = null;
try {
Field field = generator.getClass().getDeclaredField("face");
field.setAccessible(true);
face = (FreeType.Face) field.get(generator);
} catch (Exception e) {
e.printStackTrace();
return;
}

// set general font data
FreeType.SizeMetrics fontMetrics = face.getSize().getMetrics();

// Set space glyph.
Glyph spaceGlyph = data.getGlyph(' ');
if (spaceGlyph == null) {
spaceGlyph = new Glyph();
spaceGlyph.xadvance = (int) data.spaceWidth;//.spaceXadvance;
spaceGlyph.id = (int) ' ';
data.setGlyph(' ', spaceGlyph);
}
if (spaceGlyph.width == 0)
spaceGlyph.width = (int) (spaceGlyph.xadvance + data.padRight);

// set general font data
data.flipped = parameter.flip;
data.ascent = FreeType.toInt(fontMetrics.getAscender());
data.descent = FreeType.toInt(fontMetrics.getDescender());
data.lineHeight = FreeType.toInt(fontMetrics.getHeight());

// determine x-height
for (char xChar : data.xChars) {
if (!face.loadChar(xChar, FreeType.FT_LOAD_DEFAULT))
continue;
data.xHeight = FreeType.toInt(face.getGlyph().getMetrics().getHeight());
break;
}
if (data.xHeight == 0)
throw new GdxRuntimeException("No x-height character found in font");
for (char capChar : data.capChars) {
if (!face.loadChar(capChar, FreeType.FT_LOAD_DEFAULT))
continue;
data.capHeight = FreeType.toInt(face.getGlyph().getMetrics().getHeight());
break;
}

// determine cap height
if (data.capHeight == 1)
throw new GdxRuntimeException("No cap character found in font");
data.ascent = data.ascent - data.capHeight;
data.down = -data.lineHeight;
if (parameter.flip) {
data.ascent = -data.ascent;
data.down = -data.down;
}

}

@Override
public void dispose() {
setOwnsTexture(true);
super.dispose();
data.dispose();

}

public static class LazyBitmapFontData extends FreeTypeFontGenerator.FreeTypeBitmapFontData {

private FreeTypeFontGenerator generator;
private int fontSize;
private LazyBitmapFont font;
private int page = 1;

public LazyBitmapFontData(FreeTypeFontGenerator generator, int fontSize, LazyBitmapFont lbf) {
this.generator = generator;
this.fontSize = fontSize;
this.font = lbf;
}

public Glyph getGlyph(char ch) {
Glyph glyph = super.getGlyph(ch);
if (glyph == null && ch != 0)
glyph = generateGlyph(ch);
return glyph;
}

protected Glyph generateGlyph(char ch) {
FreeTypeFontGenerator.GlyphAndBitmap gab = generator.generateGlyphAndBitmap(ch, fontSize, false);
if (gab == null || gab.bitmap == null)
return null;

Pixmap map = gab.bitmap.getPixmap(Pixmap.Format.RGBA8888, Color.WHITE, 1);
TextureRegion rg = new TextureRegion(new Texture(map));
map.dispose();

font.getRegions().add(rg);

gab.glyph.page = page++;
super.setGlyph(ch, gab.glyph);
setGlyphRegion(gab.glyph, rg);

return gab.glyph;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public static void registerTags (JsonTagRegistrar registrar) {
registrar.register("ShaderAsset", ShaderAsset.class);
registrar.register("SoundAsset", SoundAsset.class);
registrar.register("TextureRegionAsset", TextureRegionAsset.class);
registrar.register("TextureAsset", TextureAsset.class);
registrar.register("TtfFontAsset", TtfFontAsset.class);

registrar.register("Transform", Transform.class);
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def visuiOnly = true
def visuiOnly = false

if (visuiOnly) {
rootProject.name = 'vis-ui'
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions ui/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ sourceCompatibility = 1.6

dependencies {
compile "com.badlogicgames.gdx:gdx:$gdxVersion"
compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
provided "com.apple:AppleJavaExtensions:$appleJavaExtensionsVersion", optional
testCompile "com.badlogicgames.gdx:gdx-backend-lwjgl3:$gdxVersion"
testCompile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
Expand Down
Loading