com.sun.javafx.font.coretext.CTFontStrike Maven / Gradle / Ivy
/*
* Copyright (c) 2013, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.javafx.font.coretext;
import com.sun.javafx.font.DisposerRecord;
import com.sun.javafx.font.FontResource;
import com.sun.javafx.font.FontStrikeDesc;
import com.sun.javafx.font.Glyph;
import com.sun.javafx.font.PrismFontFactory;
import com.sun.javafx.font.PrismFontStrike;
import com.sun.javafx.geom.Path2D;
import com.sun.javafx.geom.transform.BaseTransform;
class CTFontStrike extends PrismFontStrike {
private long fontRef;
CGAffineTransform matrix, imatrix;
boolean subPixel = false;
private static final boolean SUBPIXEL;
static {
SUBPIXEL = PrismFontFactory.getFontFactory().isSubPixelEnabled();
}
CTFontStrike(CTFontFile fontResource, float size,
BaseTransform graphicsTransform, int aaMode,
FontStrikeDesc desc) {
super(fontResource, size, graphicsTransform, aaMode, desc);
float maxDim = 80f;
if (graphicsTransform.isTranslateOrIdentity()) {
drawShapes = size > maxDim;
} else {
BaseTransform tx2d = getTransform();
matrix = new CGAffineTransform();
matrix.a = tx2d.getMxx();
matrix.b = -tx2d.getMyx(); /*Inverted coordinates system */
matrix.c = -tx2d.getMxy(); /*Inverted coordinates system */
matrix.d = tx2d.getMyy();
imatrix = OS.CGAffineTransformInvert(matrix);
if (Math.abs(matrix.a * size) > maxDim ||
Math.abs(matrix.b * size) > maxDim ||
Math.abs(matrix.c * size) > maxDim ||
Math.abs(matrix.d * size) > maxDim)
{
drawShapes = true;
}
}
long psNameRef = OS.CFStringCreate(fontResource.getPSName());
fontRef = OS.CTFontCreateWithName(psNameRef, size, matrix);
OS.CFRelease(psNameRef);
/* CoreText uses different precision for subpixel text according
* to the font size. By observation, font sizes smaller than 12
* have 4 subpixel positions. Between 12 and 17 it decreases to 3.
* Between 18 and 33 it is only 2. Above 33 it rounds all positions
* to integral values.
*/
if (SUBPIXEL && matrix == null) {
if (getAAMode() == FontResource.AA_LCD) {
/* Prism support 3 subpixel positions for LCD text and it is better
* to use Prism when possible to save texture space in the glyph
* cache.
*/
subPixel = getSize() < 12;
} else {
/* Prism has no subpixel support for grayscale text. */
subPixel = getSize() < 18;
}
}
}
long getFontRef() {
return fontRef;
}
@Override protected DisposerRecord createDisposer(FontStrikeDesc desc) {
CTFontFile fontResource = getFontResource();
return new CTStrikeDisposer(fontResource, desc, fontRef);
}
@Override protected Glyph createGlyph(int glyphCode) {
return new CTGlyph(this, glyphCode, drawShapes);
}
@Override public boolean isSubPixelGlyph() {
return subPixel;
}
@Override protected Path2D createGlyphOutline(int glyphCode) {
CTFontFile fontResource = getFontResource();
return fontResource.getGlyphOutline(glyphCode, getSize());
}
CGRect getBBox(int glyphCode) {
CTFontFile fontResource = getFontResource();
return fontResource.getBBox(glyphCode, getSize());
}
}