Skip to content

Commit

Permalink
Revised according to review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
toshiona committed Jan 5, 2022
1 parent 86d56fc commit e5d3a03
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 106 deletions.
55 changes: 26 additions & 29 deletions src/java.desktop/macosx/classes/sun/lwawt/macosx/CTextPipe.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -84,7 +84,7 @@ public void drawString(final SunGraphics2D sg2d, final String s, final double x,
private boolean hasSlotData(GlyphVector gv) {
final int length = gv.getNumGlyphs();
for (int i = 0; i < length; i++) {
if (gv.getGlyphCode(i) >= 0x1000000) { // SLOTMASK 0xff000000
if ((gv.getGlyphCode(i) & CompositeGlyphMapper.SLOTMASK) != 0) {
return true;
}
}
Expand All @@ -107,7 +107,7 @@ private GlyphVector getGlyphVectorWithRange(final Font font, final GlyphVector g
final int length = gV.getNumGlyphs();
int[] glyphs = new int[count];
for (int i = 0; i < count; i++) {
glyphs[i] = gV.getGlyphCode(start+i) & 0xFFFFFF;
glyphs[i] = gV.getGlyphCode(start+i) & CompositeGlyphMapper.GLYPHMASK;
}
// Positions should be null to recalculate by native methods,
// if GV was segmented.
Expand All @@ -120,6 +120,18 @@ private GlyphVector getGlyphVectorWithRange(final Font font, final GlyphVector g
return sgv;
}

private int getLengthOfSameSlot(final GlyphVector gV, final int targetSlot, final int start, final int length) {
int count = 1;
for (; start + count < length; count++) {
int slot = (gV.getGlyphCode(start + count) &
CompositeGlyphMapper.SLOTMASK) >> 24;
if (targetSlot != slot) {
break;
}
}
return count;
}

private void drawGlyphVectorImpl(final SunGraphics2D sg2d, final GlyphVector gV, final float x, final float y) {
final long nativeStrikePtr = getNativeStrikePtr(sg2d);
if (OSXSurfaceData.IsSimpleColor(sg2d.paint) && nativeStrikePtr != 0) {
Expand All @@ -135,36 +147,21 @@ public void drawGlyphVector(final SunGraphics2D sg2d, final GlyphVector gV, fina
sg2d.setFont(gV.getFont());

if (hasSlotData(gV)) {
int currentSlot = 0;
final int length = gV.getNumGlyphs();
float[] positions = gV.getGlyphPositions(0, length, null);
int start = 0;
int count = 0;
GlyphVector rangeGV;
for (int i = 0; i < length; i++) {
int slot = gV.getGlyphCode(i) >> 24;
if (slot == currentSlot) {
count++;
} else {
if (i > 0) {
rangeGV = getGlyphVectorWithRange(sg2d.getFont(), gV,
start, count);
drawGlyphVectorImpl(sg2d,
rangeGV,
x + positions[start * 2],
y + positions[start * 2 + 1]);
}
start = i;
count = 1;
sg2d.setFont(getSlotFont(gV.getFont(), slot));
currentSlot = slot;
}
while (start < length) {
int slot = (gV.getGlyphCode(start) &
CompositeGlyphMapper.SLOTMASK) >> 24;
sg2d.setFont(getSlotFont(gV.getFont(), slot));
int count = getLengthOfSameSlot(gV, slot, start, length);
GlyphVector rangeGV = getGlyphVectorWithRange(sg2d.getFont(),
gV, start, count);
drawGlyphVectorImpl(sg2d, rangeGV,
x + positions[start * 2],
y + positions[start * 2 + 1]);
start += count;
}
rangeGV = getGlyphVectorWithRange(sg2d.getFont(), gV, start, count);
drawGlyphVectorImpl(sg2d,
rangeGV,
x + positions[start * 2],
y + positions[start * 2 + 1]);
} else {
drawGlyphVectorImpl(sg2d, gV, x, y);
}
Expand Down
128 changes: 51 additions & 77 deletions test/jdk/java/awt/font/GlyphVector/MultiSlotFontTest.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -31,6 +31,7 @@
*/

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
Expand All @@ -40,121 +41,94 @@
import java.awt.image.BufferedImage;
import sun.font.StandardGlyphVector;
import sun.java2d.OSXOffScreenSurfaceData;
import sun.java2d.OSXSurfaceData;
import sun.java2d.SunGraphics2D;
import sun.java2d.SurfaceData;
import sun.java2d.loops.SurfaceType;

public class MultiSlotFontTest {

private static final int width = 100;
private static final int height = 60;
private static final int LIMIT = 5;
private StandardGlyphVector gv;
private static final int WIDTH = 100;
private static final int HEIGHT = 60;

private static final String[] TEST_STRINGS = {
"\u3042\u3044\u3046\u3048\u304A",
"a\u3042b\u3044c\u3046d",
"\u3042abcd",
"abcd\u3042",
};
private static final String TEST_STR = "\u3042\u3044\u3046\u3048\u304Aabc";
private static final int EXPECTED_HEIGHT = 10;
private static final int EXPECTED_WIDTH = 77;
private static final int LIMIT_DIFF_HEIGHT = 3;
private static final int LIMIT_DIFF_WIDTH = 15;

public static void main(String[] args) throws Exception {
MultiSlotFontTest test = new MultiSlotFontTest();
}

public MultiSlotFontTest() {
BufferedImage img1, img2;
BufferedImage img = createImage();

for (String str: TEST_STRINGS) {
img1 = createImage();
img2 = createImage();

callDrawGlyphVector(img1, str);
callDrawString(img2, str);

int diff = compareImages(img1, img2);
if (diff > LIMIT) {
debugOut(img1, img2);
throw new RuntimeException(
"Incorrect GlyphVector shape " +
diff + "," + str + "," + gv);
}
}
}

private void callDrawGlyphVector(BufferedImage image, String str) {
SurfaceData sd = OSXOffScreenSurfaceData.createDataIC(image,
SurfaceData sd = OSXOffScreenSurfaceData.createDataIC(img,
SurfaceType.IntRgb);
SunGraphics2D g2d = new SunGraphics2D(sd,
Color.BLACK, Color.WHITE, null);
FontRenderContext frc = new FontRenderContext(null, false, false);
Font font = g2d.getFont();
gv = new StandardGlyphVector(font, str, frc);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_OFF);
g2d.drawGlyphVector(gv, 0.0f, (float)(height - 5));
g2d.dispose();
}

private void callDrawString(BufferedImage image, String str) {
SurfaceData sd = OSXOffScreenSurfaceData.createDataIC(image,
SurfaceType.IntRgb);
SunGraphics2D g2d = new SunGraphics2D(sd,
Color.BLACK, Color.WHITE, null);
if (font.canDisplayUpTo(TEST_STR) != -1) {
System.out.println("There is no capable font. Skipping the test.");
System.out.println("Font: " + font);
return;
}

FontRenderContext frc = new FontRenderContext(null, false, false);
StandardGlyphVector gv = new StandardGlyphVector(font, TEST_STR, frc);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_OFF);
g2d.drawString(str, 0.0f, (float)(height - 5));
g2d.drawGlyphVector(gv, 0.0f, (float)(HEIGHT - 5));
g2d.dispose();

Dimension d = getBounds(img);

if (Math.abs(d.height - EXPECTED_HEIGHT) > LIMIT_DIFF_HEIGHT ||
Math.abs(d.width - EXPECTED_WIDTH) > LIMIT_DIFF_WIDTH) {
debugOut(img);
throw new RuntimeException(
"Incorrect GlyphVector shape " + d + "," + gv);
}
}

private static BufferedImage createImage() {
BufferedImage image = new BufferedImage(width, height,
BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.createGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
g.fillRect(0, 0, WIDTH, HEIGHT);
g.dispose();
return image;
}

private int getPixcelCount(BufferedImage img) {
int count = 0;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
private Dimension getBounds(BufferedImage img) {
int top = HEIGHT;
int left = WIDTH;
int right = 0;
int bottom = 0;
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
if ((img.getRGB(x, y) & 0xFFFFFF) == 0) {
count++;
if (top > y) top = y;
if (bottom < y) bottom = y;
if (left > x) left = x;
if (right < x) right = x;
}
}
}
return count;
return new Dimension(right - left, bottom - top);
}

private int compareImages(BufferedImage img1, BufferedImage img2) {
// Since positions can be shifted, check pixcel count.
int count1 = getPixcelCount(img1);
int count2 = getPixcelCount(img2);
return Math.abs(count1-count2);
}

private void debugOut(BufferedImage img1, BufferedImage img2) {
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int c1 = img1.getRGB(x, y) & 0xFFFFFF;
int c2 = img2.getRGB(x, y) & 0xFFFFFF;
if (c1 != c2) {
if (c1==0) {
System.out.print("+");
} else {
System.out.print("*");
}
private void debugOut(BufferedImage img) {
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
int c = img.getRGB(x, y) & 0xFFFFFF;
if (c == 0) {
System.out.print("*");
} else {
if (c1==0) {
System.out.print(".");
} else {
System.out.print(" ");
}
}
System.out.print(" ");
}
}
System.out.println();
}
Expand Down

0 comments on commit e5d3a03

Please sign in to comment.