New JS method global.defaultFont.measureString().

git-svn-id: http://svn.berlios.de/svnroot/repos/oolite-linux/trunk@4129 127b21dd-08f5-0310-b4b7-95ae10353056
This commit is contained in:
Jens Ayton 2011-01-23 18:24:52 +00:00
parent 68c0a3c5b3
commit 4ef5f4a8b0
9 changed files with 129 additions and 12 deletions

View File

@ -342,7 +342,8 @@ OOLITE_SCRIPTING_FILES = \
OOPListScript.m \
OOScript.m \
OOScriptTimer.m \
OOJSFrameCallbacks.m
OOJSFrameCallbacks.m \
OOJSFont.m
OOLITE_SOUND_FILES = \
OOBasicSoundReferencePoint.m \

View File

@ -83,6 +83,8 @@
1A11F84B0F35F60C001C886C /* OOJSShipGroup.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A11F8490F35F60C001C886C /* OOJSShipGroup.h */; };
1A127F4312EC6A4400B65D9F /* OOTextureSprite.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A127F4112EC6A4400B65D9F /* OOTextureSprite.h */; };
1A127F4412EC6A4400B65D9F /* OOTextureSprite.m in Sources */ = {isa = PBXBuildFile; fileRef = 1A127F4212EC6A4400B65D9F /* OOTextureSprite.m */; };
1A1280F812ECA4ED00B65D9F /* OOJSFont.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A1280F612ECA4ED00B65D9F /* OOJSFont.h */; };
1A1280F912ECA4ED00B65D9F /* OOJSFont.m in Sources */ = {isa = PBXBuildFile; fileRef = 1A1280F712ECA4ED00B65D9F /* OOJSFont.m */; };
1A143A4811EF22C5001BAB8D /* JAPersistentFileReference.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A143A4611EF22C5001BAB8D /* JAPersistentFileReference.h */; };
1A143A4911EF22C5001BAB8D /* JAPersistentFileReference.m in Sources */ = {isa = PBXBuildFile; fileRef = 1A143A4711EF22C5001BAB8D /* JAPersistentFileReference.m */; };
1A1502F60C1201C30032F3E8 /* oolite-unknown-ship.dat in Copy Models */ = {isa = PBXBuildFile; fileRef = 1A1502F50C1201C30032F3E8 /* oolite-unknown-ship.dat */; };
@ -1295,6 +1297,8 @@
1A11F8490F35F60C001C886C /* OOJSShipGroup.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OOJSShipGroup.h; sourceTree = "<group>"; };
1A127F4112EC6A4400B65D9F /* OOTextureSprite.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OOTextureSprite.h; sourceTree = "<group>"; };
1A127F4212EC6A4400B65D9F /* OOTextureSprite.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OOTextureSprite.m; sourceTree = "<group>"; };
1A1280F612ECA4ED00B65D9F /* OOJSFont.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OOJSFont.h; sourceTree = "<group>"; };
1A1280F712ECA4ED00B65D9F /* OOJSFont.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OOJSFont.m; sourceTree = "<group>"; };
1A143A4611EF22C5001BAB8D /* JAPersistentFileReference.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JAPersistentFileReference.h; sourceTree = "<group>"; };
1A143A4711EF22C5001BAB8D /* JAPersistentFileReference.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JAPersistentFileReference.m; sourceTree = "<group>"; };
1A1502F50C1201C30032F3E8 /* oolite-unknown-ship.dat */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = "oolite-unknown-ship.dat"; sourceTree = "<group>"; };
@ -2604,13 +2608,15 @@
1AC0F29D12E1DADC00ECBBB0 /* OOJSEngineDebuggerHelpers.m */,
1A11C2AF11CFC35000F3EE77 /* OOJSEngineTimeManagement.h */,
1A11C2B011CFC35000F3EE77 /* OOJSEngineTimeManagement.m */,
1A35257012E1FFA900244C9D /* OOConstToJSString.h */,
1A35257112E1FFA900244C9D /* OOConstToJSString.m */,
1ABC47FC0F155F0500B977AD /* OOJSFunction.h */,
1ABC47FD0F155F0500B977AD /* OOJSFunction.m */,
1A5DBA9C0BC000DC00D57389 /* OOJSScript.h */,
1A5DBA9D0BC000DC00D57389 /* OOJSScript.m */,
1A43A0D10CB91D3600D0E239 /* Entities */,
1A28AA0F0D55434800BC0CE4 /* Sound */,
1A28AA130D55435300BC0CE4 /* Global */,
1A5DBA9C0BC000DC00D57389 /* OOJSScript.h */,
1A5DBA9D0BC000DC00D57389 /* OOJSScript.m */,
1A736C7D0C61FD220097AC37 /* OOJSCall.h */,
1A736C7E0C61FD220097AC37 /* OOJSCall.m */,
1A3AFF1D0BC4462200B5E2D9 /* OOJSVector.h */,
@ -2625,8 +2631,8 @@
1A11F8480F35F60C001C886C /* OOJSShipGroup.m */,
1A0942C712D7C011003B6273 /* OOJSFrameCallbacks.h */,
1A0942C812D7C011003B6273 /* OOJSFrameCallbacks.m */,
1A35257012E1FFA900244C9D /* OOConstToJSString.h */,
1A35257112E1FFA900244C9D /* OOConstToJSString.m */,
1A1280F612ECA4ED00B65D9F /* OOJSFont.h */,
1A1280F712ECA4ED00B65D9F /* OOJSFont.m */,
);
name = JavaScript;
sourceTree = "<group>";
@ -3437,6 +3443,7 @@
1AEF57D312E51DDB00546444 /* OOJSEngineNativeWrappers.h in Headers */,
1AEF57D912E51EAB00546444 /* OOJSEngineTransitionHelpers.h in Headers */,
1A127F4312EC6A4400B65D9F /* OOTextureSprite.h in Headers */,
1A1280F812ECA4ED00B65D9F /* OOJSFont.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@ -3905,6 +3912,7 @@
1AC0F29E12E1DADC00ECBBB0 /* OOJSEngineDebuggerHelpers.m in Sources */,
1A35257312E1FFA900244C9D /* OOConstToJSString.m in Sources */,
1A127F4412EC6A4400B65D9F /* OOTextureSprite.m in Sources */,
1A1280F912ECA4ED00B65D9F /* OOJSFont.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

View File

@ -962,7 +962,7 @@ static OOTextureSprite *NewTextureSpriteWithDescriptor(NSDictionary *descriptor)
// Start loading the texture, and return nil if it doesn't exist.
if (result != nil && ![self preloadGUITexture:result])
{
OOJSReportWarning(context, @"%@: texture %@ could not be found.", callerDescription, [result oo_stringForKey:@"name"]);
OOJSReportWarning(context, @"%@: texture \"%@\" could not be found.", callerDescription, [result oo_stringForKey:@"name"]);
result = nil;
}

View File

@ -287,3 +287,4 @@ void OODrawHilightedString(NSString *text, double x, double y, double z, NSSize
void OODrawPlanetInfo(int gov, int eco, int tec, double x, double y, double z, NSSize siz);
void OODrawHilightedPlanetInfo(int gov, int eco, int tec, double x, double y, double z, NSSize siz);
NSRect OORectFromString(NSString *text, double x, double y, NSSize siz);
OOCGFloat OOStringWidthInEm(NSString *text);

View File

@ -49,6 +49,7 @@ MA 02110-1301, USA.
#define ONE_SIXTEENTH 0.0625
#define ONE_SIXTYFOURTH 0.015625
#define DEFAULT_OVERALL_ALPHA 0.75
#define GLYPH_SCALE_FACTOR 0.13 // // 0.13 is an inherited magic number
#define IDENTIFY_SCANNER_LOLLIPOPS ( 0 && !defined(NDEBUG))
static void DrawSpecialOval(GLfloat x, GLfloat y, GLfloat z, NSSize siz, GLfloat step, GLfloat* color4v);
@ -2731,7 +2732,7 @@ static void InitTextEngine(void)
if (count > 256) count = 256;
for (i = 0; i != count; ++i)
{
sGlyphWidths[i] = [widths oo_floatAtIndex:i] * 0.13; // 0.13 is an inherited magic number
sGlyphWidths[i] = [widths oo_floatAtIndex:i] * GLYPH_SCALE_FACTOR;
}
}
@ -2775,6 +2776,13 @@ NSRect OORectFromString(NSString *text, double x, double y, NSSize siz)
return NSMakeRect(x, y, w, siz.height);
}
OOCGFloat OOStringWidthInEm(NSString *text)
{
return OORectFromString(text, 0, 0, NSMakeSize(1.0 / (GLYPH_SCALE_FACTOR * 8.0), 1.0)).size.width;
}
void drawHighlight(double x, double y, double z, NSSize siz, double alpha)
{
// Rounded corners, fading 'shadow' version

View File

@ -0,0 +1,36 @@
/*
OOJSFont.h
JavaScript interface for introspecting fonts. Well, the font. Designed so that
expanding to multiple font support would be reasonably elegant from the JS
side of the fence.
Copyright (C) 2011 Jens Ayton
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#import <Foundation/Foundation.h>
#import <jsapi.h>
void InitOOJSFont(JSContext *context, JSObject *global);

View File

@ -0,0 +1,61 @@
/*
OOJSFont.m
Copyright (C) 2011 Jens Ayton
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#import "OOJSFont.h"
#import "OOJavaScriptEngine.h"
#import "HeadUpDisplay.h"
static JSBool FontMeasureString(OOJS_NATIVE_ARGS);
// MARK: Public
void InitOOJSFont(JSContext *context, JSObject *global)
{
JSObject *fontObject = JS_DefineObject(context, global, "defaultFont", NULL, NULL, JSPROP_PERMANENT | JSPROP_ENUMERATE | JSPROP_READONLY);
JS_DefineFunction(context, fontObject, "measureString", FontMeasureString, 1, JSPROP_PERMANENT | JSPROP_READONLY);
}
// MARK: Methods
static JSBool FontMeasureString(OOJS_NATIVE_ARGS)
{
OOJS_NATIVE_ENTER(context)
if (EXPECT_NOT(argc < 1) || JSVAL_IS_VOID(OOJS_ARG(0)))
{
jsval undefined = JSVAL_VOID;
OOJSReportBadArguments(context, nil, @"defaultFont.measureString", 1, &undefined, nil, @"string");
return NO;
}
OOJS_RETURN_DOUBLE(OOStringWidthInEm(OOStringFromJSValue(context, OOJS_ARG(0))));
OOJS_NATIVE_EXIT
}

View File

@ -116,9 +116,9 @@ static void RunDeferredOperations(JSContext *context);
void InitOOJSFrameCallbacks(JSContext *context, JSObject *global)
{
JS_DefineFunction(context, global, "addFrameCallback", GlobalAddFrameCallback, 1, 0);
JS_DefineFunction(context, global, "removeFrameCallback", GlobalRemoveFrameCallback, 1, 0);
JS_DefineFunction(context, global, "isValidFrameCallback", GlobalIsValidFrameCallback, 1, 0);
JS_DefineFunction(context, global, "addFrameCallback", GlobalAddFrameCallback, 1, JSPROP_PERMANENT | JSPROP_READONLY);
JS_DefineFunction(context, global, "removeFrameCallback", GlobalRemoveFrameCallback, 1, JSPROP_PERMANENT | JSPROP_READONLY);
JS_DefineFunction(context, global, "isValidFrameCallback", GlobalIsValidFrameCallback, 1, JSPROP_PERMANENT | JSPROP_READONLY);
#if DEBUG_FCB_SIMPLE_TRACKING_IDS
sNextID = 1;

View File

@ -62,6 +62,7 @@ MA 02110-1301, USA.
#import "OOJSEquipmentInfo.h"
#import "OOJSShipGroup.h"
#import "OOJSFrameCallbacks.h"
#import "OOJSFont.h"
#import "OOProfilingStopwatch.h"
#import "OOLoggingExtended.h"
@ -362,14 +363,15 @@ static void ReportJSError(JSContext *context, const char *message, JSErrorReport
InitOOJSEquipmentInfo(mainContext, globalObject);
InitOOJSShipGroup(mainContext, globalObject);
InitOOJSFrameCallbacks(mainContext, globalObject);
JS_EndRequest(mainContext);
InitOOJSFont(mainContext, globalObject);
// Run prefix scripts.
[OOJSScript jsScriptFromFileNamed:@"oolite-global-prefix.js"
properties:[NSDictionary dictionaryWithObject:JSSpecialFunctionsObjectWrapper(mainContext)
forKey:@"special"]];
JS_EndRequest(mainContext);
OOLog(@"script.javaScript.init.success", @"Set up JavaScript context.");
return self;