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

Second try for utf support. #532

Open
wants to merge 2 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
14 changes: 6 additions & 8 deletions luaj-2.0.3/src/core/org/luaj/vm2/Buffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
******************************************************************************/
package org.luaj.vm2;

import java.nio.charset.StandardCharsets;

/**
* String buffer for use in string library methods, optimized for production
Expand Down Expand Up @@ -166,21 +167,18 @@ public final Buffer append( LuaString str ) {
* @see LuaString#encodeToUtf8(char[], byte[], int)
*/
public final Buffer append( String str ) {
char[] chars = str.toCharArray();
/* DAN200 START */
/*
char[] chars = str.toCharArray();
final int n = LuaString.lengthAsUtf8( chars );
makeroom( 0, n );
LuaString.encodeToUtf8( chars, bytes, offset + length );
length += n;
*/
makeroom( 0, chars.length );
for( int i=0; i<chars.length; ++i )
{
char ch = chars[i];
bytes[ offset + length + i ] = (ch < 256) ? (byte)ch : (byte)'?';
}
length += chars.length;
byte[] append = str.getBytes(StandardCharsets.UTF_8);
makeroom( 0, append.length );
System.arraycopy(append, 0, bytes, offset+length, append.length);
length += append.length;
/* DAN200 END */
return this;
}
Expand Down
104 changes: 46 additions & 58 deletions luaj-2.0.3/src/core/org/luaj/vm2/LuaString.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.io.InputStream;
import java.lang.String;
import java.lang.ref.WeakReference;
import java.nio.charset.StandardCharsets;
import java.util.Hashtable;

import org.luaj.vm2.lib.MathLib;
Expand Down Expand Up @@ -93,20 +94,15 @@ private final static void index_set(Hashtable indextable, Object key, LuaString
public static LuaString valueOf(String string) {
LuaString s = index_get( index_java, string );
if ( s != null ) return s;
char[] c = string.toCharArray();
/* DAN200 START */
/*
char[] c = string.toCharArray();
byte[] b = new byte[lengthAsUtf8(c)];
encodeToUtf8(c, b, 0);
s = valueOf(b, 0, b.length);
*/
byte[] b = new byte[c.length];
for( int i=0; i<b.length; ++i )
{
char ch = c[i];
b[i] = (ch < 256) ? (byte)ch : (byte)'?';
}
s = valueOf(string.getBytes(StandardCharsets.UTF_8));
/* DAN200 END */
s = valueOf(b, 0, b.length);
index_set( index_java, string, s );
return s;
}
Expand Down Expand Up @@ -189,12 +185,7 @@ public String tojstring() {
/*
return decodeAsUtf8(m_bytes, m_offset, m_length);
*/
char[] chars = new char[ m_length ];
for( int i=0; i<chars.length; ++i )
{
chars[i] = (char)(m_bytes[ m_offset + i ] & 255);
}
return new String( chars );
return new String(m_bytes, m_offset, m_length, StandardCharsets.UTF_8);
/* DAN200 END */
}

Expand Down Expand Up @@ -561,25 +552,24 @@ public int lastIndexOf( LuaString s ) {
* @see #isValidUtf8()
*/
/* DAN200 START */
//public static String decodeAsUtf8(byte[] bytes, int offset, int length) {
private static String decodeAsUtf8(byte[] bytes, int offset, int length) {
// public static String decodeAsUtf8(byte[] bytes, int offset, int length) {
// int i,j,n,b;
// for ( i=offset,j=offset+length,n=0; i<j; ++n ) {
// switch ( 0xE0 & bytes[i++] ) {
// case 0xE0: ++i;
// case 0xC0: ++i;
// }
// }
// char[] chars=new char[n];
// for ( i=offset,j=offset+length,n=0; i<j; ) {
// chars[n++] = (char) (
// ((b=bytes[i++])>=0||i>=j)? b:
// (b<-32||i+1>=j)? (((b&0x3f) << 6) | (bytes[i++]&0x3f)):
// (((b&0xf) << 12) | ((bytes[i++]&0x3f)<<6) | (bytes[i++]&0x3f)));
// }
// return new String(chars);
// }
/* DAN200 END */
int i,j,n,b;
for ( i=offset,j=offset+length,n=0; i<j; ++n ) {
switch ( 0xE0 & bytes[i++] ) {
case 0xE0: ++i;
case 0xC0: ++i;
}
}
char[] chars=new char[n];
for ( i=offset,j=offset+length,n=0; i<j; ) {
chars[n++] = (char) (
((b=bytes[i++])>=0||i>=j)? b:
(b<-32||i+1>=j)? (((b&0x3f) << 6) | (bytes[i++]&0x3f)):
(((b&0xf) << 12) | ((bytes[i++]&0x3f)<<6) | (bytes[i++]&0x3f)));
}
return new String(chars);
}

/**
* Count the number of bytes required to encode the string as UTF-8.
Expand All @@ -590,16 +580,15 @@ private static String decodeAsUtf8(byte[] bytes, int offset, int length) {
* @see #isValidUtf8()
*/
/* DAN200 START */
//public static int lengthAsUtf8(char[] chars) {
private static int lengthAsUtf8(char[] chars) {
// public static int lengthAsUtf8(char[] chars) {
// int i,b;
// char c;
// for ( i=b=chars.length; --i>=0; )
// if ( (c=chars[i]) >=0x80 )
// b += (c>=0x800)? 2: 1;
// return b;
// }
/* DAN200 END */
int i,b;
char c;
for ( i=b=chars.length; --i>=0; )
if ( (c=chars[i]) >=0x80 )
b += (c>=0x800)? 2: 1;
return b;
}

/**
* Encode the given Java string as UTF-8 bytes, writing the result to bytes
Expand All @@ -615,24 +604,23 @@ private static int lengthAsUtf8(char[] chars) {
* @see #isValidUtf8()
*/
/* DAN200 START */
//public static void encodeToUtf8(char[] chars, byte[] bytes, int off) {
private static void encodeToUtf8(char[] chars, byte[] bytes, int off) {
// public static void encodeToUtf8(char[] chars, byte[] bytes, int off) {
// final int n = chars.length;
// char c;
// for ( int i=0, j=off; i<n; i++ ) {
// if ( (c = chars[i]) < 0x80 ) {
// bytes[j++] = (byte) c;
// } else if ( c < 0x800 ) {
// bytes[j++] = (byte) (0xC0 | ((c>>6) & 0x1f));
// bytes[j++] = (byte) (0x80 | ( c & 0x3f));
// } else {
// bytes[j++] = (byte) (0xE0 | ((c>>12) & 0x0f));
// bytes[j++] = (byte) (0x80 | ((c>>6) & 0x3f));
// bytes[j++] = (byte) (0x80 | ( c & 0x3f));
// }
// }
// }
/* DAN200 END */
final int n = chars.length;
char c;
for ( int i=0, j=off; i<n; i++ ) {
if ( (c = chars[i]) < 0x80 ) {
bytes[j++] = (byte) c;
} else if ( c < 0x800 ) {
bytes[j++] = (byte) (0xC0 | ((c>>6) & 0x1f));
bytes[j++] = (byte) (0x80 | ( c & 0x3f));
} else {
bytes[j++] = (byte) (0xE0 | ((c>>12) & 0x0f));
bytes[j++] = (byte) (0x80 | ((c>>6) & 0x3f));
bytes[j++] = (byte) (0x80 | ( c & 0x3f));
}
}
}

/** Check that a byte sequence is valid UTF-8
* @return true if it is valid UTF-8, otherwise false
Expand Down
25 changes: 23 additions & 2 deletions src/main/java/dan200/computercraft/ComputerCraft.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import dan200.computercraft.shared.turtle.upgrades.*;
import dan200.computercraft.shared.util.*;
import io.netty.buffer.Unpooled;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
Expand Down Expand Up @@ -153,6 +154,8 @@ public class ComputerCraft
public static int maximumFilesOpen = 128;

public static int maxNotesPerTick = 8;

public static boolean utf8_enable = false;

// Blocks and Items
public static class Blocks
Expand Down Expand Up @@ -223,6 +226,8 @@ public static class Config {
public static Property maximumFilesOpen;
public static Property maxNotesPerTick;

public static Property utf8_enable;

}

// Registries
Expand Down Expand Up @@ -343,6 +348,9 @@ public void preInit( FMLPreInitializationEvent event )

Config.maxNotesPerTick = Config.config.get( Configuration.CATEGORY_GENERAL, "maxNotesPerTick", maxNotesPerTick );
Config.maxNotesPerTick.setComment( "Maximum amount of notes a speaker can play at once" );

Config.utf8_enable = Config.config.get( Configuration.CATEGORY_GENERAL, "utf8_enable", utf8_enable );
Config.utf8_enable.setComment( "Enable the \"utf8 string handling\" API during Computer startup" );

for (Property property : Config.config.getCategory( Configuration.CATEGORY_GENERAL ).getOrderedValues())
{
Expand Down Expand Up @@ -386,6 +394,8 @@ public static void syncConfig() {
turtlesCanPush = Config.turtlesCanPush.getBoolean();

maxNotesPerTick = Math.max(1, Config.maxNotesPerTick.getInt());

utf8_enable = Config.utf8_enable.getBoolean();

Config.config.save();
}
Expand All @@ -397,6 +407,12 @@ public void init( FMLInitializationEvent event )
turtleProxy.init();
}

@Mod.EventHandler
public void init( FMLPostInitializationEvent event )
{
proxy.postInit();
}

@Mod.EventHandler
public void onServerStarting( FMLServerStartingEvent event )
{
Expand Down Expand Up @@ -447,6 +463,11 @@ public static void deleteDisplayLists( int list, int range )
proxy.deleteDisplayLists( list, range );
}

public static Object getFont(final String fontName)
{
return proxy.getFont(fontName);
}

public static Object getFixedWidthFontRenderer()
{
return proxy.getFixedWidthFontRenderer();
Expand Down Expand Up @@ -954,7 +975,7 @@ public void close() throws IOException
return modClass.getClassLoader().getResourceAsStream( subPath );
}

private static File getContainingJar( Class<?> modClass )
public static File getContainingJar( Class<?> modClass )
{
String path = modClass.getProtectionDomain().getCodeSource().getLocation().getPath();
int bangIndex = path.indexOf( "!" );
Expand All @@ -979,7 +1000,7 @@ private static File getContainingJar( Class<?> modClass )
return file;
}

private static File getDebugCodeDir( Class<?> modClass )
public static File getDebugCodeDir( Class<?> modClass )
{
String path = modClass.getProtectionDomain().getCodeSource().getLocation().getPath();
int bangIndex = path.indexOf("!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@

public class FixedWidthFontRenderer
{
private static ResourceLocation font = new ResourceLocation( "computercraft", "textures/gui/term_font.png" );
public static ResourceLocation background = new ResourceLocation( "computercraft", "textures/gui/term_background.png" );

public static int FONT_HEIGHT = 9;
public static int FONT_WIDTH = 6;
public static final int FONT_HEIGHT = FontManager.LEGACY.fontHeight(); // original values
public static final int FONT_WIDTH = FontManager.LEGACY.fontWidth(); // original values

private TextureManager m_textureManager;

Expand All @@ -38,10 +38,10 @@ private static void greyscaleify( double[] rgb )
Arrays.fill( rgb, ( rgb[0] + rgb[1] + rgb[2] ) / 3.0f );
}

private void drawChar( BufferBuilder renderer, double x, double y, int index, int color, Palette p, boolean greyscale )
private void drawChar( FontDefinition fd, BufferBuilder renderer, double x, double y, int index, int color, Palette p, boolean greyscale )
{
int column = index % 16;
int row = index / 16;
int column = index % fd.charsPerLine();
int row = index / fd.charsPerLine();

double[] colour = p.getColour( 15 - color );
if(greyscale)
Expand All @@ -52,15 +52,15 @@ private void drawChar( BufferBuilder renderer, double x, double y, int index, in
float g = (float)colour[1];
float b = (float)colour[2];

int xStart = 1 + column * (FONT_WIDTH + 2);
int yStart = 1 + row * (FONT_HEIGHT + 2);

renderer.pos( x, y, 0.0 ).tex( xStart / 256.0, yStart / 256.0 ).color( r, g, b, 1.0f ).endVertex();
renderer.pos( x, y + FONT_HEIGHT, 0.0 ).tex( xStart / 256.0, (yStart + FONT_HEIGHT) / 256.0 ).color( r, g, b, 1.0f ).endVertex();
renderer.pos( x + FONT_WIDTH, y, 0.0 ).tex( (xStart + FONT_WIDTH) / 256.0, yStart / 256.0 ).color( r, g, b, 1.0f ).endVertex();
renderer.pos( x + FONT_WIDTH, y, 0.0 ).tex( (xStart + FONT_WIDTH) / 256.0, yStart / 256.0 ).color( r, g, b, 1.0f ).endVertex();
renderer.pos( x, y + FONT_HEIGHT, 0.0 ).tex( xStart / 256.0, (yStart + FONT_HEIGHT) / 256.0 ).color( r, g, b, 1.0f ).endVertex();
renderer.pos( x + FONT_WIDTH, y + FONT_HEIGHT, 0.0 ).tex( (xStart + FONT_WIDTH) / 256.0, (yStart + FONT_HEIGHT) / 256.0 ).color( r, g, b, 1.0f ).endVertex();
int xStart = 1 + column * (fd.fontWidth() + 2);
int yStart = 1 + row * (fd.fontHeight() + 2);
renderer.pos( x, y, 0.0 ).tex( xStart / fd.texWidth(), yStart / fd.texHeight() ).color( r, g, b, 1.0f ).endVertex();
renderer.pos( x, y + FONT_HEIGHT, 0.0 ).tex( xStart / fd.texWidth(), (yStart + fd.fontHeight()) / fd.texHeight() ).color( r, g, b, 1.0f ).endVertex();
renderer.pos( x + FONT_WIDTH, y, 0.0 ).tex( (xStart + fd.fontWidth()) / fd.texWidth(), yStart / fd.texHeight() ).color( r, g, b, 1.0f ).endVertex();
renderer.pos( x + FONT_WIDTH, y, 0.0 ).tex( (xStart + fd.fontWidth()) / fd.texWidth(), yStart / fd.texHeight() ).color( r, g, b, 1.0f ).endVertex();
renderer.pos( x, y + FONT_HEIGHT, 0.0 ).tex( xStart / fd.texWidth(), (yStart + fd.fontHeight()) / fd.texHeight() ).color( r, g, b, 1.0f ).endVertex();
renderer.pos( x + FONT_WIDTH, y + FONT_HEIGHT, 0.0 ).tex( (xStart + fd.fontWidth()) / fd.texWidth(), (yStart + fd.fontHeight()) / fd.texHeight() ).color( r, g, b, 1.0f ).endVertex();
}

private void drawQuad( BufferBuilder renderer, double x, double y, int color, double width, Palette p, boolean greyscale )
Expand Down Expand Up @@ -125,7 +125,7 @@ public void drawStringBackgroundPart( int x, int y, TextBuffer backgroundColour,
GlStateManager.enableTexture2D();
}

public void drawStringTextPart( int x, int y, TextBuffer s, TextBuffer textColour, boolean greyScale, Palette p )
public void drawStringTextPart( FontDefinition fd, int x, int y, TextBuffer s, TextBuffer textColour, boolean greyScale, Palette p )
{
// Draw the quads
Tessellator tessellator = Tessellator.getInstance();
Expand All @@ -142,16 +142,16 @@ public void drawStringTextPart( int x, int y, TextBuffer s, TextBuffer textColou

// Draw char
int index = (int)s.charAt( i );
if( index < 0 || index > 255 )
if( index < 0 || index > fd.maxChars() )
{
index = (int)'?';
}
drawChar( renderer, x + i * FONT_WIDTH, y, index, colour, p, greyScale );
drawChar( fd, renderer, x + i * FONT_WIDTH, y, index, colour, p, greyScale );
}
tessellator.draw();
}

public void drawString( TextBuffer s, int x, int y, TextBuffer textColour, TextBuffer backgroundColour, double leftMarginSize, double rightMarginSize, boolean greyScale, Palette p )
public void drawString( FontDefinition fd, TextBuffer s, int x, int y, TextBuffer textColour, TextBuffer backgroundColour, double leftMarginSize, double rightMarginSize, boolean greyScale, Palette p )
{
// Draw background
if( backgroundColour != null )
Expand All @@ -167,10 +167,10 @@ public void drawString( TextBuffer s, int x, int y, TextBuffer textColour, TextB
if( s != null && textColour != null )
{
// Bind the font texture
bindFont();
bindFont(fd);

// Draw the quads
drawStringTextPart( x, y, s, textColour, greyScale, p );
drawStringTextPart( fd, x, y, s, textColour, greyScale, p );
}
}

Expand All @@ -183,9 +183,9 @@ public int getStringWidth(String s)
return s.length() * FONT_WIDTH;
}

public void bindFont()
public void bindFont(FontDefinition fd)
{
m_textureManager.bindTexture( font );
m_textureManager.bindTexture( fd.font() );
GlStateManager.glTexParameteri( GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_CLAMP );
}
}
Loading