Skip to content

Commit df2f48e

Browse files
authored
Revert back standard code for frmDefault (#48)
1 parent bf599ce commit df2f48e

File tree

3 files changed

+140
-76
lines changed

3 files changed

+140
-76
lines changed

nimPDF/fontmanager.nim

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ proc GenerateWidths*(f: TTFont, renderMode: FontRenderMode): string =
122122

123123
result = "[ " & widthsParts.join(" ") & " ]"
124124

125-
of frmDefault, frmPathRendering:
125+
of frmPathRendering:
126126
# When subsetting, GIDs used are sequential newGIDs starting from 1.
127127
# Sort by newGID to ensure correct order for the widths array.
128128
f.CH2GID.sort(proc(x,y: tuple[key: int, val: TONGID]):int = cmp(x.val.newGID, y.val.newGID))
@@ -138,6 +138,19 @@ proc GenerateWidths*(f: TTFont, renderMode: FontRenderMode): string =
138138

139139
result = "[ " & $firstGID & " [ " & widthsArray.join(" ") & " ] ]"
140140

141+
of frmDefault:
142+
f.CH2GID.sort(proc(x,y: tuple[key: int, val: TONGID]):int = cmp(x.val.newGID, y.val.newGID) )
143+
var widths = "[ 1["
144+
var x = 0
145+
146+
for gid in values(f.CH2GID):
147+
widths.add($f.GetCharWidth(gid.oldGID))
148+
if x < f.CH2GID.len-1: widths.add(' ')
149+
inc(x)
150+
151+
widths.add("]]")
152+
result = widths
153+
141154
proc GenerateRanges*(f: TTFont): string =
142155
var entries: seq[string] = @[]
143156

@@ -159,18 +172,18 @@ proc GenerateRanges*(f: TTFont): string =
159172
let highSurrogate = 0xD800 + (codePoint shr 10)
160173
let lowSurrogate = 0xDC00 + (codePoint and 0x3FF)
161174
toHex(highSurrogate, 4).toUpperAscii & toHex(lowSurrogate, 4).toUpperAscii
162-
175+
163176
# ToUnicode CMap maps oldGID (used in content stream) to Unicode
164177
let entry = "<" & cidHex & "> <" & codeHex & ">"
165178
entries.add(entry)
166-
167-
of frmDefault, frmPathRendering:
179+
180+
of frmPathRendering:
168181
# For subset fonts, use the existing logic with newGID
169182
var usedCids = initHashSet[int]()
170183
for item in pairs(f.CH2GID):
171184
let code = item[0] # Unicode value
172185
let cid = item[1].newGID # For subset fonts, content stream uses newGID
173-
186+
174187
# Only include valid CIDs (> 0) and valid Unicode values
175188
if cid > 0 and code > 0 and not usedCids.contains(cid):
176189
let cidHex = toHex(cid, 4).toUpperAscii
@@ -183,19 +196,35 @@ proc GenerateRanges*(f: TTFont): string =
183196
let highSurrogate = 0xD800 + (codePoint shr 10)
184197
let lowSurrogate = 0xDC00 + (codePoint and 0x3FF)
185198
toHex(highSurrogate, 4).toUpperAscii & toHex(lowSurrogate, 4).toUpperAscii
186-
199+
187200
# ToUnicode CMap maps character IDs (used in content stream) to Unicode
188201
let entry = "<" & cidHex & "> <" & codeHex & ">"
189202
entries.add(entry)
190203
usedCids.incl(cid)
191204

205+
of frmDefault:
206+
var range: seq[string] = @[]
207+
var mapping = ""
208+
209+
for code, gid in pairs(f.CH2GID):
210+
if range.len >= 100:
211+
mapping.add("\x0A" & $range.len & " beginbfchar\x0A" & join(range, "\x0A") & "\x0Aendbfchar")
212+
range = @[]
213+
range.add("<" & toHex(gid.newGID, 4) & "><" & toHex(code, 4) & ">")
214+
215+
if range.len > 0:
216+
mapping.add("\x0A" & $range.len & " beginbfchar\x0A" & join(range, "\x0A") & "\x0Aendbfchar")
217+
218+
result = mapping
219+
192220
if entries.len > 0:
193221
result = $entries.len & " beginbfchar\n" & entries.join("\n") & "\nendbfchar\n"
194222
else:
195223
result = ""
196224

197-
proc GetDescriptor*(f: TTFont): FontDescriptor =
198-
#f.CH2GID.sort(proc(x,y: tuple[key: int, val: TONGID]):int = cmp(x.key, y.key) )
225+
proc GetDescriptor*(f: TTFont, renderMode: FontRenderMode): FontDescriptor =
226+
if renderMode == frmDefault:
227+
f.CH2GID.sort(proc(x,y: tuple[key: int, val: TONGID]):int = cmp(x.key, y.key) )
199228
result = f.font.newFontDescriptor(f.CH2GID)
200229

201230
proc GetSubsetBuffer*(f: TTFont, subsetTag: string, renderMode: FontRenderMode): string =
@@ -217,7 +246,7 @@ method CanWriteVertical*(f: Base14): bool = false
217246
method CanWriteVertical*(f: TTFont): bool =
218247
result = f.vmtx != nil
219248

220-
method EscapeString*(f: Font, text: string,): string {.base.} =
249+
method EscapeString*(f: Font, text: string): string {.base.} =
221250
discard
222251

223252

@@ -261,11 +290,18 @@ method EscapeString*(f: TTFont, text: string): string =
261290
result = ""
262291
for c in runes(text):
263292
let charCode = int(c)
264-
if f.CH2GID.hasKey(charCode):
265-
# For subsetted fonts, use the newGID in the content stream
266-
result.add(toHex(f.CH2GID[charCode].newGID, 4))
293+
if f.renderMode == frmDefault:
294+
if f.CH2GID.hasKey(charCode):
295+
let gid = f.CH2GID[charCode].newGID
296+
result.add(toHex(gid, 4))
297+
else:
298+
result.add("0000")
267299
else:
268-
result.add("0000")
300+
if f.CH2GID.hasKey(charCode):
301+
# For subsetted fonts, use the newGID in the content stream
302+
result.add(toHex(f.CH2GID[charCode].newGID, 4))
303+
else:
304+
result.add("0000")
269305

270306
method GetTextWidth*(f: Font, text: string): TextWidth {.base.} =
271307
discard
@@ -445,7 +481,6 @@ proc makeTTFont(font: FontDef, searchName: string): TTFont =
445481
var res: TTFont
446482
new(res)
447483

448-
res.ID = 0 # Initialize ID field
449484
res.subType = FT_TRUETYPE
450485
res.searchName = searchName
451486
res.font = font
@@ -455,8 +490,10 @@ proc makeTTFont(font: FontDef, searchName: string): TTFont =
455490
res.glyph = GLYPHTable(font.getTable(TAG.glyf))
456491
res.scaleFactor= 1000 / head.UnitsPerEm()
457492
res.CH2GID = initOrderedTable[int, TONGID]()
458-
res.fullCharMap = initOrderedTable[int, TONGID]() # Initialize cache
459493
res.newGID = 1
494+
495+
res.fullCharMap = initOrderedTable[int, TONGID]() # Initialize cache
496+
res.ID = 0 # Initialize ID field
460497
res.renderMode = frmDefault # Initialize with default render mode
461498

462499
# Pre-populate the full character mapping cache
@@ -534,7 +571,6 @@ proc makeFont*(
534571

535572
var searchName = family
536573
searchName.add(searchStyle)
537-
538574

539575

540576
# First check if font already exists in fontList
@@ -583,7 +619,8 @@ proc makeFont*(
583619
ff.fontList.add(fon14)
584620
return fon14
585621

586-
result = nil
622+
result = makeFont(ff, defaultFont, style, enc, renderMode)
623+
#result = nil
587624

588625
when isMainModule:
589626
var ff: FontManager

0 commit comments

Comments
 (0)