git.y1.nz

SameBoy

Accurate GB/GBC emulator
download: https://git.y1.nz/archives/sameboy.tar.gz
README | Files | Log | Refs | LICENSE

HexFiend/HFRepresenterHexTextView.m

      1 //
      2 //  HFRepresenterHexTextView.m
      3 //  HexFiend_2
      4 //
      5 //  Copyright 2007 ridiculous_fish. All rights reserved.
      6 //
      7 
      8 #import <HexFiend/HFRepresenterHexTextView.h>
      9 #import <HexFiend/HFRepresenterTextView_Internal.h>
     10 #import <HexFiend/HFHexTextRepresenter.h>
     11 
     12 @implementation HFRepresenterHexTextView
     13 
     14 - (void)generateGlyphTable {
     15     const UniChar hexchars[17] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F',' '/* Plus a space char at the end for null bytes. */};
     16     _Static_assert(sizeof(CGGlyph[17]) == sizeof(glyphTable), "glyphTable is the wrong type");
     17     NSFont *font = [[self font] screenFont];
     18     
     19     bool t = CTFontGetGlyphsForCharacters((CTFontRef)font, hexchars, glyphTable, 17);
     20     HFASSERT(t); // We don't take kindly to strange fonts around here.
     21     
     22     CGFloat maxAdv = 0.0;
     23     for(int i = 0; i < 17; i++) maxAdv = HFMax(maxAdv, [font advancementForGlyph:glyphTable[i]].width);
     24     glyphAdvancement = maxAdv;
     25     spaceAdvancement = maxAdv;
     26 }
     27 
     28 - (void)setFont:(NSFont *)font {
     29     [super setFont:font];
     30     [self generateGlyphTable];
     31 }
     32 
     33 - (instancetype)initWithCoder:(NSCoder *)coder {
     34     HFASSERT([coder allowsKeyedCoding]);
     35     self = [super initWithCoder:coder];
     36     [self generateGlyphTable];
     37     return self;
     38 }
     39 
     40 //no need for encodeWithCoder
     41 
     42 - (void)extractGlyphsForBytes:(const unsigned char *)bytes count:(NSUInteger)numBytes offsetIntoLine:(NSUInteger)offsetIntoLine intoArray:(struct HFGlyph_t *)glyphs advances:(CGSize *)advances resultingGlyphCount:(NSUInteger *)resultGlyphCount {
     43     HFASSERT(bytes != NULL);
     44     HFASSERT(glyphs != NULL);
     45     HFASSERT(numBytes <= NSUIntegerMax);
     46     HFASSERT(resultGlyphCount != NULL);
     47     const NSUInteger bytesPerColumn = [self bytesPerColumn];
     48     NSUInteger glyphIndex = 0, byteIndex = 0;
     49     NSUInteger remainingBytesInThisColumn = (bytesPerColumn ? bytesPerColumn - offsetIntoLine % bytesPerColumn : NSUIntegerMax);
     50     CGFloat advanceBetweenColumns = [self advanceBetweenColumns];
     51     while (byteIndex < numBytes) {
     52         unsigned char byte = bytes[byteIndex++];
     53         
     54         CGFloat glyphAdvancementPlusAnySpace = glyphAdvancement;
     55         if (--remainingBytesInThisColumn == 0) {
     56             remainingBytesInThisColumn = bytesPerColumn;
     57             glyphAdvancementPlusAnySpace += advanceBetweenColumns;
     58         }
     59         
     60         BOOL useBlank = (hidesNullBytes && byte == 0);
     61         advances[glyphIndex] = CGSizeMake(glyphAdvancement, 0);
     62         glyphs[glyphIndex++] = (struct HFGlyph_t){.fontIndex = 0, .glyph = glyphTable[(useBlank? 16: byte >> 4)]};
     63         advances[glyphIndex] = CGSizeMake(glyphAdvancementPlusAnySpace, 0);
     64         glyphs[glyphIndex++] = (struct HFGlyph_t){.fontIndex = 0, .glyph = glyphTable[(useBlank? 16: byte & 0xF)]};
     65     }
     66     
     67     *resultGlyphCount = glyphIndex;
     68 }
     69 
     70 - (CGFloat)advancePerCharacter {
     71     return 2 * glyphAdvancement;
     72 }
     73 
     74 - (CGFloat)advanceBetweenColumns {
     75     return glyphAdvancement;
     76 }
     77 
     78 - (NSUInteger)maximumGlyphCountForByteCount:(NSUInteger)byteCount {
     79     return 2 * byteCount;
     80 }
     81 
     82 - (BOOL)hidesNullBytes {
     83     return hidesNullBytes;
     84 }
     85 
     86 - (void)setHidesNullBytes:(BOOL)flag
     87 {
     88     flag = !! flag;
     89     if (hidesNullBytes != flag) {
     90         hidesNullBytes = flag;
     91         [self setNeedsDisplay:YES];
     92     }
     93 }
     94 
     95 @end

This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.