mac fonts sort of work now
- haven't tested rotation yet - spacing/bounds still look bad git-svn-id: http://skia.googlecode.com/svn/trunk@117 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
parent
0bf64d48cc
commit
330578d67d
91
samplecode/SampleFontScalerTest.cpp
Normal file
91
samplecode/SampleFontScalerTest.cpp
Normal file
@ -0,0 +1,91 @@
|
||||
#include "SampleCode.h"
|
||||
#include "SkView.h"
|
||||
#include "SkCanvas.h"
|
||||
#include "SkTypeface.h"
|
||||
#include "SkPath.h"
|
||||
#include "SkRegion.h"
|
||||
#include "SkShader.h"
|
||||
#include "SkUtils.h"
|
||||
#include "Sk1DPathEffect.h"
|
||||
#include "SkCornerPathEffect.h"
|
||||
#include "SkPathMeasure.h"
|
||||
#include "SkRandom.h"
|
||||
#include "SkColorPriv.h"
|
||||
#include "SkColorFilter.h"
|
||||
#include "SkDither.h"
|
||||
|
||||
static const struct {
|
||||
const char* fName;
|
||||
SkTypeface::Style fStyle;
|
||||
} gFaces[] = {
|
||||
{ NULL, SkTypeface::kNormal },
|
||||
{ NULL, SkTypeface::kBold },
|
||||
{ "serif", SkTypeface::kNormal },
|
||||
{ "serif", SkTypeface::kBold },
|
||||
{ "serif", SkTypeface::kItalic },
|
||||
{ "serif", SkTypeface::kBoldItalic },
|
||||
{ "monospace", SkTypeface::kNormal }
|
||||
};
|
||||
|
||||
static const int gFaceCount = SK_ARRAY_COUNT(gFaces);
|
||||
|
||||
class FontScalerTestView : public SkView {
|
||||
SkTypeface* fFaces[gFaceCount];
|
||||
|
||||
public:
|
||||
FontScalerTestView() {
|
||||
for (int i = 0; i < gFaceCount; i++) {
|
||||
fFaces[i] = SkTypeface::CreateFromName(gFaces[i].fName,
|
||||
gFaces[i].fStyle);
|
||||
}
|
||||
}
|
||||
|
||||
virtual ~FontScalerTestView() {
|
||||
for (int i = 0; i < gFaceCount; i++) {
|
||||
fFaces[i]->safeUnref();
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
// overrides from SkEventSink
|
||||
virtual bool onQuery(SkEvent* evt) {
|
||||
if (SampleCode::TitleQ(*evt)) {
|
||||
SampleCode::TitleR(evt, "FontScaler Test");
|
||||
return true;
|
||||
}
|
||||
return this->INHERITED::onQuery(evt);
|
||||
}
|
||||
|
||||
void drawBG(SkCanvas* canvas) {
|
||||
canvas->drawColor(0xFFDDDDDD);
|
||||
}
|
||||
|
||||
virtual void onDraw(SkCanvas* canvas) {
|
||||
this->drawBG(canvas);
|
||||
|
||||
SkPaint paint;
|
||||
paint.setAntiAlias(true);
|
||||
paint.setTypeface(SkTypeface::CreateFromName("Times Roman", SkTypeface::kNormal))->safeUnref();
|
||||
|
||||
const char* text = "HHHaaammmbbbuuurrrgggeeefffooonnnsss";
|
||||
const size_t textLen = strlen(text);
|
||||
|
||||
SkScalar x = SkIntToScalar(10);
|
||||
SkScalar y = SkIntToScalar(20);
|
||||
|
||||
for (int ps = 9; ps <= 24; ps++) {
|
||||
paint.setTextSize(SkIntToScalar(ps));
|
||||
canvas->drawText(text, textLen, x, y, paint);
|
||||
y += paint.getFontMetrics(NULL);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
typedef SkView INHERITED;
|
||||
};
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
static SkView* MyFactory() { return new FontScalerTestView; }
|
||||
static SkViewRegister reg(MyFactory);
|
||||
|
@ -185,20 +185,64 @@ void SkScalerContext_Mac::generateAdvance(SkGlyph* glyph) {
|
||||
|
||||
void SkScalerContext_Mac::generateMetrics(SkGlyph* glyph) {
|
||||
GlyphID glyphID = glyph->getGlyphID(fBaseGlyphCount);
|
||||
ATSGlyphScreenMetrics metrics;
|
||||
ATSGlyphScreenMetrics screenMetrics;
|
||||
ATSGlyphIdealMetrics idealMetrics;
|
||||
|
||||
OSStatus err = ATSUGlyphGetScreenMetrics(fStyle, 1, &glyphID, 0, true, true,
|
||||
&metrics);
|
||||
&screenMetrics);
|
||||
if (noErr != err) {
|
||||
set_glyph_metrics_on_error(glyph);
|
||||
} else {
|
||||
glyph->fAdvanceX = SkFloatToFixed(metrics.deviceAdvance.x);
|
||||
glyph->fAdvanceY = -SkFloatToFixed(metrics.deviceAdvance.y);
|
||||
glyph->fWidth = metrics.width;
|
||||
glyph->fHeight = metrics.height;
|
||||
glyph->fLeft = sk_float_round2int(metrics.topLeft.x);
|
||||
glyph->fTop = -sk_float_round2int(metrics.topLeft.y);
|
||||
return;
|
||||
}
|
||||
err = ATSUGlyphGetIdealMetrics(fStyle, 1, &glyphID, 0, &idealMetrics);
|
||||
if (noErr != err) {
|
||||
set_glyph_metrics_on_error(glyph);
|
||||
return;
|
||||
}
|
||||
|
||||
if (kNormal_Hints == fRec.fHints) {
|
||||
glyph->fAdvanceX = SkFloatToFixed(screenMetrics.deviceAdvance.x);
|
||||
glyph->fAdvanceY = -SkFloatToFixed(screenMetrics.deviceAdvance.y);
|
||||
} else {
|
||||
glyph->fAdvanceX = SkFloatToFixed(idealMetrics.advance.x);
|
||||
glyph->fAdvanceY = -SkFloatToFixed(idealMetrics.advance.y);
|
||||
}
|
||||
|
||||
// specify an extra 1-pixel border, go tive CG room for its antialiasing
|
||||
// i.e. without this, I was seeing some edges chopped off!
|
||||
glyph->fWidth = screenMetrics.width + 2;
|
||||
glyph->fHeight = screenMetrics.height + 2;
|
||||
glyph->fLeft = sk_float_round2int(screenMetrics.topLeft.x) - 1;
|
||||
glyph->fTop = -sk_float_round2int(screenMetrics.topLeft.y) - 1;
|
||||
}
|
||||
|
||||
void SkScalerContext_Mac::generateImage(const SkGlyph& glyph)
|
||||
{
|
||||
SkAutoMutexAcquire ac(gFTMutex);
|
||||
SkASSERT(fLayout);
|
||||
|
||||
bzero(glyph.fImage, glyph.fHeight * glyph.rowBytes());
|
||||
CGContextRef contextRef = ::CGBitmapContextCreate(glyph.fImage,
|
||||
glyph.fWidth, glyph.fHeight, 8,
|
||||
glyph.rowBytes(), fGrayColorSpace,
|
||||
kCGImageAlphaNone);
|
||||
if (!contextRef) {
|
||||
SkASSERT(false);
|
||||
return;
|
||||
}
|
||||
|
||||
::CGContextSetGrayFillColor(contextRef, 1.0, 1.0);
|
||||
::CGContextSetTextDrawingMode(contextRef, kCGTextFill);
|
||||
|
||||
CGGlyph glyphID = glyph.getGlyphID();
|
||||
CGFontRef fontRef = CGFontCreateWithPlatformFont(&fRec.fFontID);
|
||||
CGContextSetFont(contextRef, fontRef);
|
||||
CGContextSetFontSize(contextRef, 1);
|
||||
CGContextSetTextMatrix(contextRef, fTransform);
|
||||
CGContextShowGlyphsAtPoint(contextRef, -glyph.fLeft,
|
||||
glyph.fTop + glyph.fHeight, &glyphID, 1);
|
||||
|
||||
::CGContextRelease(contextRef);
|
||||
}
|
||||
|
||||
static void convert_metrics(SkPaint::FontMetrics* dst,
|
||||
@ -315,35 +359,6 @@ void SkScalerContext_Mac::generateFontMetrics(SkPaint::FontMetrics* mx,
|
||||
}
|
||||
}
|
||||
|
||||
void SkScalerContext_Mac::generateImage(const SkGlyph& glyph)
|
||||
{
|
||||
SkAutoMutexAcquire ac(gFTMutex);
|
||||
SkASSERT(fLayout);
|
||||
|
||||
bzero(glyph.fImage, glyph.fHeight * glyph.rowBytes());
|
||||
CGContextRef contextRef = ::CGBitmapContextCreate(glyph.fImage,
|
||||
glyph.fWidth, glyph.fHeight, 8,
|
||||
glyph.rowBytes(), fGrayColorSpace,
|
||||
kCGImageAlphaNone);
|
||||
if (!contextRef) {
|
||||
SkASSERT(false);
|
||||
return;
|
||||
}
|
||||
|
||||
::CGContextSetGrayFillColor(contextRef, 1.0, 1.0);
|
||||
::CGContextSetTextDrawingMode(contextRef, kCGTextFill);
|
||||
|
||||
CGGlyph glyphID = glyph.getGlyphID();
|
||||
CGFontRef fontRef = CGFontCreateWithPlatformFont(&fRec.fFontID);
|
||||
CGContextSetFont(contextRef, fontRef);
|
||||
CGContextSetFontSize(contextRef, 1); //SkScalarToFloat(fRec.fTextSize));
|
||||
CGContextSetTextMatrix(contextRef, fTransform);
|
||||
CGContextShowGlyphsAtPoint(contextRef, -glyph.fLeft,
|
||||
glyph.fTop + glyph.fHeight, &glyphID, 1);
|
||||
|
||||
::CGContextRelease(contextRef);
|
||||
}
|
||||
|
||||
void SkScalerContext_Mac::generatePath(const SkGlyph& glyph, SkPath* path)
|
||||
{
|
||||
SkAutoMutexAcquire ac(gFTMutex);
|
||||
|
@ -65,6 +65,7 @@
|
||||
007A7CC00F01658C00A2D6EE /* SampleVertices.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 007A7CB10F01658C00A2D6EE /* SampleVertices.cpp */; };
|
||||
007A7CC10F01658C00A2D6EE /* SampleXfermodes.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 007A7CB20F01658C00A2D6EE /* SampleXfermodes.cpp */; };
|
||||
007C785E0F3B4C230004B142 /* SamplePathClip.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 007C785D0F3B4C230004B142 /* SamplePathClip.cpp */; };
|
||||
009CC9190F65918A002185BE /* SampleFontScalerTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 009CC9180F65918A002185BE /* SampleFontScalerTest.cpp */; };
|
||||
00A41E4B0EFC312F00C9CBEB /* SampleArc.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 00A41E4A0EFC312F00C9CBEB /* SampleArc.cpp */; };
|
||||
0156F80407C56A3000C6122B /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0156F80307C56A3000C6122B /* Foundation.framework */; };
|
||||
01FC44D507BD3BB800D228F4 /* Quartz.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 01FC44D407BD3BB800D228F4 /* Quartz.framework */; };
|
||||
@ -178,6 +179,7 @@
|
||||
007A7CB10F01658C00A2D6EE /* SampleVertices.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = SampleVertices.cpp; path = ../../samplecode/SampleVertices.cpp; sourceTree = SOURCE_ROOT; };
|
||||
007A7CB20F01658C00A2D6EE /* SampleXfermodes.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = SampleXfermodes.cpp; path = ../../samplecode/SampleXfermodes.cpp; sourceTree = SOURCE_ROOT; };
|
||||
007C785D0F3B4C230004B142 /* SamplePathClip.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = SamplePathClip.cpp; path = ../../samplecode/SamplePathClip.cpp; sourceTree = SOURCE_ROOT; };
|
||||
009CC9180F65918A002185BE /* SampleFontScalerTest.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = SampleFontScalerTest.cpp; path = ../../samplecode/SampleFontScalerTest.cpp; sourceTree = SOURCE_ROOT; };
|
||||
00A41E4A0EFC312F00C9CBEB /* SampleArc.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = SampleArc.cpp; path = ../../samplecode/SampleArc.cpp; sourceTree = SOURCE_ROOT; };
|
||||
0156F80307C56A3000C6122B /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = /System/Library/Frameworks/Foundation.framework; sourceTree = "<absolute>"; };
|
||||
01FC44D407BD3BB800D228F4 /* Quartz.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Quartz.framework; path = /System/Library/Frameworks/Quartz.framework; sourceTree = "<absolute>"; };
|
||||
@ -212,6 +214,7 @@
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
007A7CA40F01658C00A2D6EE /* SamplePicture.cpp */,
|
||||
009CC9180F65918A002185BE /* SampleFontScalerTest.cpp */,
|
||||
007A7CA50F01658C00A2D6EE /* SamplePoints.cpp */,
|
||||
007A7CA70F01658C00A2D6EE /* SampleRegion.cpp */,
|
||||
007A7CA80F01658C00A2D6EE /* SampleShaders.cpp */,
|
||||
@ -509,6 +512,7 @@
|
||||
0041CE3C0F00A12400695E8C /* SampleEncode.cpp in Sources */,
|
||||
007A7CB60F01658C00A2D6EE /* SampleRegion.cpp in Sources */,
|
||||
007C785E0F3B4C230004B142 /* SamplePathClip.cpp in Sources */,
|
||||
009CC9190F65918A002185BE /* SampleFontScalerTest.cpp in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user