1 /*
   2  * Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.lwawt.macosx;
  27 
  28 
  29 import java.awt.*;
  30 import java.awt.font.*;
  31 
  32 import sun.awt.*;
  33 import sun.font.*;
  34 import sun.java2d.*;
  35 import sun.java2d.loops.*;
  36 import sun.java2d.pipe.*;
  37 
  38 public class CTextPipe implements TextPipe {
  39     public native void doDrawString(SurfaceData sData, long nativeStrikePtr, String s, double x, double y);
  40     public native void doDrawGlyphs(SurfaceData sData, long nativeStrikePtr, GlyphVector gV, float x, float y);
  41     public native void doUnicodes(SurfaceData sData, long nativeStrikePtr, char[] unicodes, int offset, int length, float x, float y);
  42     public native void doOneUnicode(SurfaceData sData, long nativeStrikePtr, char aUnicode, float x, float y);
  43 
  44     long getNativeStrikePtr(final SunGraphics2D sg2d) {
  45         final FontStrike fontStrike = sg2d.getFontInfo().fontStrike;
  46         if (!(fontStrike instanceof CStrike)) return 0;
  47         return ((CStrike)fontStrike).getNativeStrikePtr();
  48     }
  49 
  50     void drawGlyphVectorAsShape(final SunGraphics2D sg2d, final GlyphVector gv, final float x, final float y) {
  51         final int length = gv.getNumGlyphs();
  52         for (int i = 0; i < length; i++) {
  53             final Shape glyph = gv.getGlyphOutline(i, x, y);
  54             sg2d.fill(glyph);
  55         }
  56     }
  57 
  58     void drawTextAsShape(final SunGraphics2D sg2d, final String s, final double x, final double y) {
  59         final Object oldAliasingHint = sg2d.getRenderingHint(SunHints.KEY_ANTIALIASING);
  60         final FontRenderContext frc = sg2d.getFontRenderContext();
  61         sg2d.setRenderingHint(SunHints.KEY_ANTIALIASING, (frc.isAntiAliased() ? SunHints.VALUE_ANTIALIAS_ON : SunHints.VALUE_ANTIALIAS_OFF));
  62 
  63         final Font font = sg2d.getFont();
  64         final GlyphVector gv = font.createGlyphVector(frc, s);
  65         final int length = gv.getNumGlyphs();
  66         for (int i = 0; i < length; i++) {
  67             final Shape glyph = gv.getGlyphOutline(i, (float)x, (float)y);
  68             sg2d.fill(glyph);
  69         }
  70 
  71         sg2d.setRenderingHint(SunHints.KEY_ANTIALIASING, oldAliasingHint);
  72     }
  73 
  74     public void drawString(final SunGraphics2D sg2d, final String s, final double x, final double y) {
  75         final long nativeStrikePtr = getNativeStrikePtr(sg2d);
  76         if (OSXSurfaceData.IsSimpleColor(sg2d.paint) && nativeStrikePtr != 0) {
  77             final OSXSurfaceData surfaceData = (OSXSurfaceData)sg2d.getSurfaceData();
  78             surfaceData.drawString(this, sg2d, nativeStrikePtr, s, x, y);
  79         } else {
  80             drawTextAsShape(sg2d, s, x, y);
  81         }
  82     }
  83 
  84     public void drawGlyphVector(final SunGraphics2D sg2d, final GlyphVector gV, final float x, final float y) {
  85         final Font prevFont = sg2d.getFont();
  86         sg2d.setFont(gV.getFont());
  87 
  88         final long nativeStrikePtr = getNativeStrikePtr(sg2d);
  89         if (OSXSurfaceData.IsSimpleColor(sg2d.paint) && nativeStrikePtr != 0) {
  90             final OSXSurfaceData surfaceData = (OSXSurfaceData)sg2d.getSurfaceData();
  91             surfaceData.drawGlyphs(this, sg2d, nativeStrikePtr, gV, x, y);
  92         } else {
  93             drawGlyphVectorAsShape(sg2d, gV, x, y);
  94         }
  95         sg2d.setFont(prevFont);
  96     }
  97 
  98     public void drawChars(final SunGraphics2D sg2d, final char[] data, final int offset, final int length, final int x, final int y) {
  99         final long nativeStrikePtr = getNativeStrikePtr(sg2d);
 100         if (OSXSurfaceData.IsSimpleColor(sg2d.paint) && nativeStrikePtr != 0) {
 101             final OSXSurfaceData surfaceData = (OSXSurfaceData)sg2d.getSurfaceData();
 102             surfaceData.drawUnicodes(this, sg2d, nativeStrikePtr, data, offset, length, x, y);
 103         } else {
 104             drawTextAsShape(sg2d, new String(data, offset, length), x, y);
 105         }
 106     }
 107 
 108     public CTextPipe traceWrap() {
 109         return new Tracer();
 110     }
 111 
 112     public static class Tracer extends CTextPipe {
 113         void doDrawString(final SurfaceData sData, final long nativeStrikePtr, final String s, final float x, final float y) {
 114             GraphicsPrimitive.tracePrimitive("QuartzDrawString");
 115             super.doDrawString(sData, nativeStrikePtr, s, x, y);
 116         }
 117 
 118         public void doDrawGlyphs(final SurfaceData sData, final long nativeStrikePtr, final GlyphVector gV, final float x, final float y) {
 119             GraphicsPrimitive.tracePrimitive("QuartzDrawGlyphs");
 120             super.doDrawGlyphs(sData, nativeStrikePtr, gV, x, y);
 121         }
 122 
 123         public void doUnicodes(final SurfaceData sData, final long nativeStrikePtr, final char[] unicodes, final int offset, final int length, final float x, final float y) {
 124             GraphicsPrimitive.tracePrimitive("QuartzDrawUnicodes");
 125             super.doUnicodes(sData, nativeStrikePtr, unicodes, offset, length, x, y);
 126         }
 127 
 128         public void doOneUnicode(final SurfaceData sData, final long nativeStrikePtr, final char aUnicode, final float x, final float y) {
 129             GraphicsPrimitive.tracePrimitive("QuartzDrawUnicode");
 130             super.doOneUnicode(sData, nativeStrikePtr, aUnicode, x, y);
 131         }
 132     }
 133 }