OSDN Git Service

For English spell descriptions, most only use one space after a period. Changed...
[hengbandforosx/hengbandosx.git] / src / main-cocoa.m
index dff0286..f4a2163 100644 (file)
@@ -32,7 +32,6 @@
 #define kVK_Escape 0x35
 #define kVK_ANSI_KeypadEnter 0x4C
 
-static NSSize const AngbandScaleIdentity = {1.0, 1.0};
 static NSString * const AngbandDirectoryNameLib = @"lib";
 static NSString * const AngbandDirectoryNameBase = @"Hengband";
 
@@ -334,9 +333,10 @@ static int resize_pending_changes(struct PendingChanges* pc, int nrow)
 
 
 /* The max number of glyphs we support.  Currently this only affects
- * updateGlyphInfo() for the calculation of the tile size (the glyphArray and
- * glyphWidths members of AngbandContext are only used in updateGlyphInfo()).
- * The rendering in drawWChar will work for glyphs not in updateGlyphInfo()'s
+ * updateGlyphInfo() for the calculation of the tile size, fontAscender,
+ * fontDescender, ncol_pre, and ncol_post (the glyphArray and glyphWidths
+ * members of AngbandContext are only used in updateGlyphInfo()).  The
+ * rendering in drawWChar will work for glyphs not in updateGlyphInfo()'s
  * set, and that is used for rendering Japanese characters.
  */
 #define GLYPH_COUNT 256
@@ -463,6 +463,9 @@ static int resize_pending_changes(struct PendingChanges* pc, int nrow)
  * defaults */
 - (void)resizeTerminalWithContentRect: (NSRect)contentRect saveToDefaults: (BOOL)saveToDefaults;
 
+/* Change the minimum size for the window associated with the context. */
+- (void)setMinimumWindowSize;
+
 /* Called from the view to indicate that it is starting or ending live resize */
 - (void)viewWillStartLiveResize:(AngbandView *)view;
 - (void)viewDidEndLiveResize:(AngbandView *)view;
@@ -770,18 +773,27 @@ static int compare_advances(const void *ap, const void *bp)
     }
     
     /*
-     * Record the ascender and descender.  Adjust both by 0.5 pixel to leave
-     * space for antialiasing/subpixel positioning.
+     * Record the ascender and descender.  Some fonts, for instance DIN
+     * Condensed and Rockwell in 10.14, the ascent on '@' exceeds that
+     * reported by [screenFont ascender].  Get the overall bounding box
+     * for the glyphs and use that instead of the ascender and descender
+     * values if the bounding box result extends farther from the baseline.
      */
-    fontAscender = [screenFont ascender] + 0.5;
-    fontDescender = [screenFont descender] - 0.5;
+    CGRect bounds = CTFontGetBoundingRectsForGlyphs((CTFontRef) screenFont, kCTFontHorizontalOrientation, glyphArray, NULL, GLYPH_COUNT);
+    fontAscender = [screenFont ascender];
+    if (fontAscender < bounds.origin.y + bounds.size.height) {
+       fontAscender = bounds.origin.y + bounds.size.height;
+    }
+    fontDescender = [screenFont descender];
+    if (fontDescender > bounds.origin.y) {
+       fontDescender = bounds.origin.y;
+    }
 
     /*
-     * Record the tile size.  Add one to the median advance to leave space
-     * for antialiasing/subpixel positioning.  Round both values up to
-     * have tile boundaries match pixel boundaries.
+     * Record the tile size.  Round both values up to have tile boundaries
+     * match pixel boundaries.
      */
-    tileSize.width = ceil(medianAdvance + 1.);
+    tileSize.width = ceil(medianAdvance);
     tileSize.height = ceil(fontAscender - fontDescender);
 
     /*
@@ -1007,7 +1019,7 @@ static int compare_advances(const void *ap, const void *bp)
 
     textMatrix = CGAffineTransformScale( textMatrix, 1.0, -1.0 );
     CGContextSetTextMatrix(ctx, textMatrix);
-    CGContextShowGlyphsWithAdvances(ctx, &glyph, &CGSizeZero, 1);
+    CGContextShowGlyphsAtPositions(ctx, &glyph, &CGPointZero, 1);
     
     /* Restore the text matrix if we messed with the compression ratio */
     if (compressionRatio != 1.)
@@ -1075,6 +1087,23 @@ static int compare_advances(const void *ap, const void *bp)
         /* Adjust terminal to fit window with new font; save the new columns
                 * and rows since they could be changed */
         NSRect contentRect = [self->primaryWindow contentRectForFrameRect: [self->primaryWindow frame]];
+
+       [self setMinimumWindowSize];
+       NSSize size = self->primaryWindow.contentMinSize;
+       BOOL windowNeedsResizing = NO;
+       if (contentRect.size.width < size.width) {
+           contentRect.size.width = size.width;
+           windowNeedsResizing = YES;
+       }
+       if (contentRect.size.height < size.height) {
+           contentRect.size.height = size.height;
+           windowNeedsResizing = YES;
+       }
+       if (windowNeedsResizing) {
+           size.width = contentRect.size.width;
+           size.height = contentRect.size.height;
+           [self->primaryWindow setContentSize:size];
+       }
         [self resizeTerminalWithContentRect: contentRect saveToDefaults: YES];
     }
 
@@ -1646,6 +1675,24 @@ static NSMenuItem *superitem(NSMenuItem *self)
     Term_activate( old );
 }
 
+- (void)setMinimumWindowSize
+{
+    NSSize minsize;
+
+    if ([self terminalIndex] == 0) {
+       minsize.width = 80;
+       minsize.height = 24;
+    } else {
+       minsize.width = 1;
+       minsize.height = 1;
+    }
+    minsize.width =
+       minsize.width * self->tileSize.width + self->borderSize.width * 2.0;
+    minsize.height =
+        minsize.height * self->tileSize.height + self->borderSize.height * 2.0;
+    [[self makePrimaryWindow] setContentMinSize:minsize];
+}
+
 - (void)saveWindowVisibleToDefaults: (BOOL)windowVisible
 {
        int termIndex = [self terminalIndex];
@@ -1859,7 +1906,7 @@ static void record_current_savefile(void)
     {
         NSUserDefaults *angbandDefs = [NSUserDefaults angbandDefaults];
         [angbandDefs setObject:savefileString forKey:@"SaveFile"];
-        [angbandDefs synchronize];        
+        [angbandDefs synchronize];
     }
 }
 
@@ -1960,43 +2007,16 @@ static void Term_init_cocoa(term *t)
 
     /* Get the window */
     NSWindow *window = [context makePrimaryWindow];
-    NSString *title;
-
     /* Set its title and, for auxiliary terms, tentative size */
-    if (termIdx == 0)
-    {
-       title = [NSString stringWithCString:angband_term_name[0]
+    NSString *title = [NSString stringWithCString:angband_term_name[termIdx]
 #ifdef JP
-                             encoding:NSJapaneseEUCStringEncoding
+                               encoding:NSJapaneseEUCStringEncoding
 #else
-                             encoding:NSMacOSRomanStringEncoding
+                               encoding:NSMacOSRomanStringEncoding
 #endif
-       ];
-        [window setTitle:title];
-
-        /* Set minimum size (80x24) */
-        NSSize minsize;
-        minsize.width = 80 * context->tileSize.width + context->borderSize.width * 2.0;
-        minsize.height = 24 * context->tileSize.height + context->borderSize.height * 2.0;
-        [window setContentMinSize:minsize];
-    }
-    else
-    {
-       title = [NSString stringWithCString:angband_term_name[termIdx]
-#ifdef JP
-                             encoding:NSJapaneseEUCStringEncoding
-#else
-                             encoding:NSMacOSRomanStringEncoding
-#endif
-       ];
-        [window setTitle:title];
-        /* Set minimum size (1x1) */
-        NSSize minsize;
-        minsize.width = context->tileSize.width + context->borderSize.width * 2.0;
-        minsize.height = context->tileSize.height + context->borderSize.height * 2.0;
-        [window setContentMinSize:minsize];
-    }
-    
+    ];
+    [window setTitle:title];
+    [context setMinimumWindowSize];
     
     /* If this is the first term, and we support full screen (Mac OS X Lion or
         * later), then allow it to go full screen (sweet). Allow other terms to be
@@ -2581,7 +2601,7 @@ static void Term_xtra_cocoa_fresh(AngbandContext* angbandContext)
                            prc->cell_changes[jx].a;
                        sourceRect.size.width = graf_width;
                        sourceRect.size.height = graf_height;
-                       terrainRect.origin.x = graf_width * 
+                       terrainRect.origin.x = graf_width *
                            prc->cell_changes[jx].tcol;
                        terrainRect.origin.y = graf_height *
                            prc->cell_changes[jx].trow;
@@ -2601,7 +2621,7 @@ static void Term_xtra_cocoa_fresh(AngbandContext* angbandContext)
                                pict_image,
                                sourceRect,
                                destinationRect,
-                               NSCompositeSourceOver); 
+                               NSCompositeSourceOver);
                        } else {
                            draw_image_tile(
                                nsContext,
@@ -2746,7 +2766,7 @@ static void Term_xtra_cocoa_fresh(AngbandContext* angbandContext)
        [angbandContext setNeedsDisplayInBaseRect:rect];
     }
 
-    [angbandContext unlockFocus];    
+    [angbandContext unlockFocus];
 }
 
 
@@ -2825,7 +2845,7 @@ static errr Term_xtra_cocoa(int n, int v)
         {        
             [angbandContext lockFocus];
             [[NSColor blackColor] set];
-            NSRect imageRect = {NSZeroPoint, [angbandContext imageSize]};            
+            NSRect imageRect = {NSZeroPoint, [angbandContext imageSize]};
             NSRectFillUsingOperation(imageRect, NSCompositeCopy);
             [angbandContext unlockFocus];
             [angbandContext setNeedsDisplay:YES];
@@ -3520,7 +3540,7 @@ static void handle_open_when_ready(void)
         game_in_progress = TRUE;
         
         /* Wait for a keypress */
-        pause_line(23);
+        pause_line(Term->hgt - 1);
     }
 }
 
@@ -4066,8 +4086,15 @@ static void hook_quit(const char * str)
         }
         else
         {
+           /*
+            * Another window is only usable after Term_init_cocoa() has
+            * been called for it.  For Angband if window_flag[i] is nonzero
+            * then that has happened for window i.  For Hengband, that is
+            * not the case so also test angband_term[i]->data.
+            */
             NSInteger subwindowNumber = tag - AngbandWindowMenuItemTagBase;
-            return (window_flag[subwindowNumber] > 0);
+            return (angband_term[subwindowNumber]->data != 0
+                   && window_flag[subwindowNumber] > 0);
         }
 
         return NO;
@@ -4135,7 +4162,14 @@ static void hook_quit(const char * str)
     NSMenu *windowsMenu = [[NSApplication sharedApplication] windowsMenu];
     [windowsMenu addItem: [NSMenuItem separatorItem]];
 
-    NSMenuItem *angbandItem = [[NSMenuItem alloc] initWithTitle: @"Hengband" action: @selector(selectWindow:) keyEquivalent: @"0"];
+    NSString *title1 = [NSString stringWithCString:angband_term_name[0]
+#ifdef JP
+                               encoding:NSJapaneseEUCStringEncoding
+#else
+                               encoding:NSMacOSRomanStringEncoding
+#endif
+    ];
+    NSMenuItem *angbandItem = [[NSMenuItem alloc] initWithTitle:title1 action: @selector(selectWindow:) keyEquivalent: @"0"];
     [angbandItem setTarget: self];
     [angbandItem setTag: AngbandWindowMenuItemTagBase];
     [windowsMenu addItem: angbandItem];
@@ -4144,7 +4178,13 @@ static void hook_quit(const char * str)
     /* Add items for the additional term windows */
     for( NSInteger i = 1; i < ANGBAND_TERM_MAX; i++ )
     {
-        NSString *title = [NSString stringWithFormat: @"Term %ld", (long)i];
+        NSString *title = [NSString stringWithCString:angband_term_name[i]
+#ifdef JP
+                                   encoding:NSJapaneseEUCStringEncoding
+#else
+                                   encoding:NSMacOSRomanStringEncoding
+#endif
+       ];
         NSString *keyEquivalent = [NSString stringWithFormat: @"%ld", (long)i];
         NSMenuItem *windowItem = [[NSMenuItem alloc] initWithTitle: title action: @selector(selectWindow:) keyEquivalent: keyEquivalent];
         [windowItem setTarget: self];
@@ -4391,7 +4431,7 @@ static void hook_quit(const char * str)
 
 int main(int argc, char* argv[])
 {
-    NSApplicationMain(argc, (void*)argv);    
+    NSApplicationMain(argc, (void*)argv);
     return (0);
 }